Skip to content

Commit 797dbc0

Browse files
committed
simplify menu state check
1 parent b8ea9b7 commit 797dbc0

File tree

1 file changed

+50
-57
lines changed

1 file changed

+50
-57
lines changed

src/menu.c

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ typedef struct dyn_item {
1212
HMENU hmenu; // submenu handle
1313
UINT id; // menu command id
1414
void *talloc_ctx; // talloc context
15-
bool (*update)(plugin_ctx *ctx, struct dyn_item *item);
15+
void (*update)(mp_state *state, struct dyn_item *item);
1616
} dyn_entry;
1717

1818
typedef struct {
@@ -22,17 +22,17 @@ typedef struct {
2222

2323
typedef struct {
2424
char *keyword; // keyword in menu title
25-
bool (*update)(plugin_ctx *ctx, dyn_entry *item);
25+
void (*update)(mp_state *state, dyn_entry *item);
2626
} dyn_provider;
2727

2828
// forward declarations
29-
static bool update_video_track_menu(plugin_ctx *ctx, dyn_entry *item);
30-
static bool update_audio_track_menu(plugin_ctx *ctx, dyn_entry *item);
31-
static bool update_sub_track_menu(plugin_ctx *ctx, dyn_entry *item);
32-
static bool update_sub_track_menu2(plugin_ctx *ctx, dyn_entry *item);
33-
static bool update_chapter_menu(plugin_ctx *ctx, dyn_entry *item);
34-
static bool update_edition_menu(plugin_ctx *ctx, dyn_entry *item);
35-
static bool update_audio_device_menu(plugin_ctx *ctx, dyn_entry *item);
29+
static void update_video_track_menu(mp_state *state, dyn_entry *item);
30+
static void update_audio_track_menu(mp_state *state, dyn_entry *item);
31+
static void update_sub_track_menu(mp_state *state, dyn_entry *item);
32+
static void update_sub_track_menu2(mp_state *state, dyn_entry *item);
33+
static void update_chapter_menu(mp_state *state, dyn_entry *item);
34+
static void update_edition_menu(mp_state *state, dyn_entry *item);
35+
static void update_audio_device_menu(mp_state *state, dyn_entry *item);
3636

3737
// dynamic menu providers
3838
static const dyn_provider dyn_providers[] = {
@@ -159,12 +159,12 @@ static HMENU append_submenu(HMENU hmenu, wchar_t *title, int *id) {
159159
return menu;
160160
}
161161

162-
static bool update_track_menu(plugin_ctx *ctx, dyn_entry *item,
162+
static void update_track_menu(mp_state *state, dyn_entry *item,
163163
const char *type, const char *prop, int64_t pos) {
164-
mp_track_list *list = ctx->state->track_list;
165-
if (list == NULL || list->num_entries == 0) return false;
164+
mp_track_list *list = state->track_list;
165+
if (list == NULL || list->num_entries == 0) return;
166166

167-
void *tmp = talloc_new(NULL);
167+
int count = GetMenuItemCount(item->hmenu);
168168

169169
for (int i = 0; i < list->num_entries; i++) {
170170
mp_track_item *entry = &list->entries[i];
@@ -181,40 +181,33 @@ static bool update_track_menu(plugin_ctx *ctx, dyn_entry *item,
181181
talloc_asprintf(item->talloc_ctx, "set %s %d", prop, entry->id));
182182
}
183183

184-
if (GetMenuItemCount(item->hmenu) == 0) {
185-
talloc_free(tmp);
186-
return false;
184+
if (GetMenuItemCount(item->hmenu) > count) {
185+
append_menu(item->hmenu, MIIM_STRING | MIIM_DATA | MIIM_STATE, 0,
186+
pos < 0 ? MFS_CHECKED : MFS_UNCHECKED,
187+
escape_title(item->talloc_ctx, bstr0("Off")), NULL,
188+
talloc_asprintf(item->talloc_ctx, "set %s no", prop));
187189
}
188-
189-
append_menu(item->hmenu, MIIM_STRING | MIIM_DATA | MIIM_STATE, 0,
190-
pos < 0 ? MFS_CHECKED : MFS_UNCHECKED,
191-
escape_title(item->talloc_ctx, bstr0("Off")), NULL,
192-
talloc_asprintf(item->talloc_ctx, "set %s no", prop));
193-
194-
talloc_free(tmp);
195-
return true;
196190
}
197191

198-
static bool update_video_track_menu(plugin_ctx *ctx, dyn_entry *item) {
199-
return update_track_menu(ctx, item, "video", "vid", ctx->state->vid);
192+
static void update_video_track_menu(mp_state *state, dyn_entry *item) {
193+
update_track_menu(state, item, "video", "vid", state->vid);
200194
}
201195

202-
static bool update_audio_track_menu(plugin_ctx *ctx, dyn_entry *item) {
203-
return update_track_menu(ctx, item, "audio", "aid", ctx->state->aid);
196+
static void update_audio_track_menu(mp_state *state, dyn_entry *item) {
197+
update_track_menu(state, item, "audio", "aid", state->aid);
204198
}
205199

206-
static bool update_sub_track_menu(plugin_ctx *ctx, dyn_entry *item) {
207-
return update_track_menu(ctx, item, "sub", "sid", ctx->state->sid);
200+
static void update_sub_track_menu(mp_state *state, dyn_entry *item) {
201+
update_track_menu(state, item, "sub", "sid", state->sid);
208202
}
209203

210-
static bool update_sub_track_menu2(plugin_ctx *ctx, dyn_entry *item) {
211-
return update_track_menu(ctx, item, "sub", "secondary-sid",
212-
ctx->state->sid2);
204+
static void update_sub_track_menu2(mp_state *state, dyn_entry *item) {
205+
update_track_menu(state, item, "sub", "secondary-sid", state->sid2);
213206
}
214207

215-
static bool update_chapter_menu(plugin_ctx *ctx, dyn_entry *item) {
216-
mp_chapter_list *list = ctx->state->chapter_list;
217-
if (list == NULL || list->num_entries == 0) return false;
208+
static void update_chapter_menu(mp_state *state, dyn_entry *item) {
209+
mp_chapter_list *list = state->chapter_list;
210+
if (list == NULL || list->num_entries == 0) return;
218211

219212
void *tmp = talloc_new(NULL);
220213

@@ -229,44 +222,40 @@ static bool update_chapter_menu(plugin_ctx *ctx, dyn_entry *item) {
229222
NULL,
230223
talloc_asprintf(item->talloc_ctx, "seek %f absolute", entry->time));
231224
}
232-
if (ctx->state->chapter >= 0)
233-
CheckMenuRadioItem(item->hmenu, 0, list->num_entries,
234-
ctx->state->chapter, MF_BYPOSITION);
225+
if (state->chapter >= 0) {
226+
CheckMenuRadioItem(item->hmenu, 0, list->num_entries, state->chapter,
227+
MF_BYPOSITION);
228+
}
235229

236230
talloc_free(tmp);
237-
return true;
238231
}
239232

240-
static bool update_edition_menu(plugin_ctx *ctx, dyn_entry *item) {
241-
mp_edition_list *list = ctx->state->edition_list;
242-
if (list == NULL || list->num_entries == 0) return false;
243-
244-
void *tmp = talloc_new(NULL);
233+
static void update_edition_menu(mp_state *state, dyn_entry *item) {
234+
mp_edition_list *list = state->edition_list;
235+
if (list == NULL || list->num_entries == 0) return;
245236

246237
int pos = -1;
247238
for (int i = 0; i < list->num_entries; i++) {
248239
mp_edition_item *entry = &list->entries[i];
249-
if (entry->id == ctx->state->edition) pos = i;
240+
if (entry->id == state->edition) pos = i;
250241
append_menu(
251242
item->hmenu, MIIM_STRING | MIIM_DATA, 0, 0,
252243
escape_title(item->talloc_ctx, bstr0(entry->title)), NULL,
253244
talloc_asprintf(item->talloc_ctx, "set edition %d", entry->id));
254245
}
255-
if (pos >= 0)
246+
if (pos >= 0) {
256247
CheckMenuRadioItem(item->hmenu, 0, list->num_entries, pos,
257248
MF_BYPOSITION);
258-
259-
talloc_free(tmp);
260-
return true;
249+
}
261250
}
262251

263-
static bool update_audio_device_menu(plugin_ctx *ctx, dyn_entry *item) {
264-
mp_audio_device_list *list = ctx->state->audio_device_list;
265-
if (list == NULL || list->num_entries == 0) return false;
252+
static void update_audio_device_menu(mp_state *state, dyn_entry *item) {
253+
mp_audio_device_list *list = state->audio_device_list;
254+
if (list == NULL || list->num_entries == 0) return;
266255

267256
void *tmp = talloc_new(NULL);
268257

269-
char *name = ctx->state->audio_device;
258+
char *name = state->audio_device;
270259
int pos = -1;
271260
for (int i = 0; i < list->num_entries; i++) {
272261
mp_audio_device *entry = &list->entries[i];
@@ -279,12 +268,12 @@ static bool update_audio_device_menu(plugin_ctx *ctx, dyn_entry *item) {
279268
talloc_asprintf(item->talloc_ctx, "set audio-device %s",
280269
entry->name));
281270
}
282-
if (pos >= 0)
271+
if (pos >= 0) {
283272
CheckMenuRadioItem(item->hmenu, 0, list->num_entries, pos,
284273
MF_BYPOSITION);
274+
}
285275

286276
talloc_free(tmp);
287-
return true;
288277
}
289278

290279
static void dyn_menu_init(void *talloc_ctx) {
@@ -302,7 +291,11 @@ static void dyn_menu_update(plugin_ctx *ctx) {
302291
RemoveMenu(item->hmenu, 0, MF_BYPOSITION);
303292
talloc_free_children(item->talloc_ctx);
304293

305-
UINT enable = item->update(ctx, item) ? MF_ENABLED : MF_GRAYED;
294+
item->update(ctx->state, item);
295+
296+
// update state
297+
int count = GetMenuItemCount(item->hmenu);
298+
UINT enable = count > 0 ? MF_ENABLED : MF_DISABLED;
306299
EnableMenuItem(ctx->hmenu, item->id, MF_BYCOMMAND | enable);
307300
}
308301
}

0 commit comments

Comments
 (0)