mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 04:58:13 +00:00
LibGUI: Refactor icon realization in Menu
This factors out icon realization into its own function, making it possible to use the same code with other classes that have icon() and set_icon() methods.
This commit is contained in:
parent
082b7d6b0c
commit
048f149f51
1 changed files with 22 additions and 15 deletions
|
@ -102,6 +102,27 @@ void Menu::dismiss()
|
||||||
WindowServerConnection::the().post_message(Messages::WindowServer::DismissMenu(m_menu_id));
|
WindowServerConnection::the().post_message(Messages::WindowServer::DismissMenu(m_menu_id));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
template<typename IconContainerType>
|
||||||
|
static int ensure_realized_icon(IconContainerType& container)
|
||||||
|
{
|
||||||
|
int icon_buffer_id = -1;
|
||||||
|
if (container.icon()) {
|
||||||
|
ASSERT(container.icon()->format() == Gfx::BitmapFormat::RGBA32);
|
||||||
|
ASSERT(container.icon()->size() == Gfx::IntSize(16, 16));
|
||||||
|
if (container.icon()->shbuf_id() == -1) {
|
||||||
|
auto shared_buffer = SharedBuffer::create_with_size(container.icon()->size_in_bytes());
|
||||||
|
ASSERT(shared_buffer);
|
||||||
|
auto shared_icon = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *shared_buffer, container.icon()->size());
|
||||||
|
memcpy(shared_buffer->data(), container.icon()->bits(0), container.icon()->size_in_bytes());
|
||||||
|
shared_buffer->seal();
|
||||||
|
shared_buffer->share_with(WindowServerConnection::the().server_pid());
|
||||||
|
container.set_icon(shared_icon);
|
||||||
|
}
|
||||||
|
icon_buffer_id = container.icon()->shbuf_id();
|
||||||
|
}
|
||||||
|
return icon_buffer_id;
|
||||||
|
}
|
||||||
|
|
||||||
int Menu::realize_menu(RefPtr<Action> default_action)
|
int Menu::realize_menu(RefPtr<Action> default_action)
|
||||||
{
|
{
|
||||||
unrealize_menu();
|
unrealize_menu();
|
||||||
|
@ -127,21 +148,7 @@ int Menu::realize_menu(RefPtr<Action> default_action)
|
||||||
}
|
}
|
||||||
if (item.type() == MenuItem::Type::Action) {
|
if (item.type() == MenuItem::Type::Action) {
|
||||||
auto& action = *item.action();
|
auto& action = *item.action();
|
||||||
int icon_buffer_id = -1;
|
int icon_buffer_id = ensure_realized_icon(action);
|
||||||
if (action.icon()) {
|
|
||||||
ASSERT(action.icon()->format() == Gfx::BitmapFormat::RGBA32);
|
|
||||||
ASSERT(action.icon()->size() == Gfx::IntSize(16, 16));
|
|
||||||
if (action.icon()->shbuf_id() == -1) {
|
|
||||||
auto shared_buffer = SharedBuffer::create_with_size(action.icon()->size_in_bytes());
|
|
||||||
ASSERT(shared_buffer);
|
|
||||||
auto shared_icon = Gfx::Bitmap::create_with_shared_buffer(Gfx::BitmapFormat::RGBA32, *shared_buffer, action.icon()->size());
|
|
||||||
memcpy(shared_buffer->data(), action.icon()->bits(0), action.icon()->size_in_bytes());
|
|
||||||
shared_buffer->seal();
|
|
||||||
shared_buffer->share_with(WindowServerConnection::the().server_pid());
|
|
||||||
action.set_icon(shared_icon);
|
|
||||||
}
|
|
||||||
icon_buffer_id = action.icon()->shbuf_id();
|
|
||||||
}
|
|
||||||
auto shortcut_text = action.shortcut().is_valid() ? action.shortcut().to_string() : String();
|
auto shortcut_text = action.shortcut().is_valid() ? action.shortcut().to_string() : String();
|
||||||
bool exclusive = action.group() && action.group()->is_exclusive() && action.is_checkable();
|
bool exclusive = action.group() && action.group()->is_exclusive() && action.is_checkable();
|
||||||
bool is_default = (default_action.ptr() == &action);
|
bool is_default = (default_action.ptr() == &action);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue