comparison foosdk/sdk/foobar2000/foo_sample/mainmenu-dynamic.cpp @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #include "stdafx.h"
2 #include <vector>
3
4 namespace { // anon namespace everything, it's not accessible by means other than the service factory
5
6 // The command ID.
7 // Generate a new GUID when reusing code.
8 static const GUID guid_menucommand = { 0xab754b0b, 0x204, 0x4471, { 0xb5, 0x29, 0xff, 0x73, 0xae, 0x51, 0x5d, 0xe9 } };
9
10 // Shared with mainmenu.cpp
11 static const GUID guid_mainmenu_group_id = { 0x44963e7a, 0x4b2a, 0x4588, { 0xb0, 0x17, 0xa8, 0x69, 0x18, 0xcb, 0x8a, 0xa5 } };
12
13 class sample_command : public mainmenu_node_command {
14 public:
15 sample_command( size_t index ) : m_index(index) {
16 }
17 void get_display(pfc::string_base & text, t_uint32 & flags) override {
18 flags = 0;
19 text = PFC_string_formatter() << "Test dynamic item #" << m_index;
20 }
21 void execute(service_ptr_t<service_base> callback) override {
22 popup_message::g_show(PFC_string_formatter() << "Invoked test menu item #" << m_index, "Information");
23 }
24 GUID get_guid() override {
25 // This method returns our subcommand ID.
26 // Dynamic commands are identified by a pair of GUIDs:
27 // - command ID ( see: mainmenu_commands interface )
28 // - subcommand ID ( identifier of one of the dynamic items )
29 // Subcommand identifiers don't have to be actually globally unique,
30 // as long as they're unique among the subcommand identifiers for this command ID.
31
32 // In our case, we'll just create a makeshift GUID from a hash of the index.
33 // This is perfectly okay for production code - as long as your command ID is a proper GUID!
34
35 // Don't ever hash size_t which varies with CPU architecture
36 // Integer endianness intentionally disregarded
37 uint32_t hashme = (uint32_t) m_index;
38
39 auto api = hasher_md5::get();
40 hasher_md5_state state;
41 api->initialize( state );
42 api->process( state, &hashme, sizeof(hashme) );
43
44 // fb2k hasher_md5 API even provides a convenient method to return MD5 hashes cast to GUIDs for this.
45 return api->get_result_guid( state );
46 }
47 bool get_description(pfc::string_base & out) override {
48 out = PFC_string_formatter() << "This is a test menu item #" << m_index << ".";
49 return true;
50 }
51 private:
52 const size_t m_index;
53 };
54 class sample_group : public mainmenu_node_group {
55 public:
56 sample_group() {
57 m_children.resize(11);
58 for( size_t walk = 0; walk < m_children.size(); ++ walk ) {
59 mainmenu_node::ptr node;
60 // Insert separators for odd items, commands for even
61 if ( walk % 2 ) {
62 node = fb2k::service_new<mainmenu_node_separator>();
63 } else {
64 auto cmdIndex = walk/2 + 1;
65 node = fb2k::service_new<sample_command>( cmdIndex );
66 }
67 m_children[walk] = std::move(node);
68 }
69 }
70 void get_display(pfc::string_base & text, t_uint32 & flags) override {
71 flags = 0;
72 text = "Dynamic menu test group";
73 }
74 t_size get_children_count() override {
75 return m_children.size();
76 }
77 mainmenu_node::ptr get_child(t_size index) override {
78 PFC_ASSERT( index < m_children.size() );
79 return m_children[index];
80 }
81 private:
82 std::vector<mainmenu_node::ptr> m_children;
83
84 };
85
86 class mainmenu_sample_dynamic : public mainmenu_commands_v2 {
87 typedef mainmenu_commands_v2 super_t;
88 public:
89 // mainmenu_commands_v2 methods
90 t_uint32 get_command_count() override { return 1; }
91 GUID get_command(t_uint32 p_index) override {return guid_menucommand;}
92 void get_name(t_uint32 p_index,pfc::string_base & p_out) override {p_out = "Dynamic menu test";}
93
94 bool get_description(t_uint32 p_index,pfc::string_base & p_out) override {
95 // Should not get here much
96 p_out = "This is a dynamic menu command test.";
97 return true;
98 }
99 GUID get_parent() override {return guid_mainmenu_group_id; }
100 void execute(t_uint32 p_index,service_ptr_t<service_base> p_callback) override {
101 // Should not get here, someone not aware of our dynamic status tried to invoke us?
102 }
103
104 bool is_command_dynamic(t_uint32 index) override { return true; }
105 mainmenu_node::ptr dynamic_instantiate(t_uint32 index) override {
106 return fb2k::service_new<sample_group>();
107 }
108
109 bool dynamic_execute(t_uint32 index, const GUID & subID, service_ptr_t<service_base> callback) override {
110 // If your component provides a more efficient way to execute the command,
111 // than doing full dynamic_instantiate() and walking all the dynamic items to find one with the matching identifier,
112 // please implement it here.
113
114 // ... or just skip implementing this method entirely.
115 return super_t::dynamic_execute( index, subID, callback );
116 }
117 };
118
119 static service_factory_single_t<mainmenu_sample_dynamic> g_mainmenu_sample_dynamic;
120
121 } // namespace