Skip to content

Commit

Permalink
feat(shell): click to show and hide submenu if dont have action
Browse files Browse the repository at this point in the history
close #57
  • Loading branch information
std-microblock committed Feb 4, 2025
1 parent bbb65fd commit 9bf7795
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 44 deletions.
98 changes: 55 additions & 43 deletions src/shell/contextmenu/menu_widget.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "menu_render.h"
#include "nanovg.h"
#include "ui.h"
#include "widget.h"
#include <algorithm>
#include <iostream>
#include <print>
Expand Down Expand Up @@ -129,6 +130,12 @@ void mb_shell::menu_item_normal_widget::update(ui::update_context &ctx) {
} catch (std::exception &e) {
std::cerr << "Error in action: " << e.what() << std::endl;
}
} else if (item.submenu) {
if (submenu_wid) {
hide_submenu();
} else {
show_submenu(ctx);
}
}
}

Expand All @@ -142,52 +149,13 @@ void mb_shell::menu_item_normal_widget::update(ui::update_context &ctx) {

if (show_submenu_timer >= 250.f) {
if (!submenu_wid) {
submenu_wid = std::make_shared<menu_widget>();
item.submenu.value()(submenu_wid);

auto anchor_x = *width + *x + ctx.offset_x;
auto anchor_y = **y + ctx.offset_y;

anchor_x *= ctx.rt.dpi_scale;
anchor_y *= ctx.rt.dpi_scale;

submenu_wid->update(ctx);
auto direction = mouse_menu_widget_main::calculate_direction(
submenu_wid.get(), ctx, anchor_x, anchor_y,
popup_direction::bottom_right);

if (direction == popup_direction::top_left ||
direction == popup_direction::bottom_left) {
anchor_x -= *width;
}

auto [x, y] = mouse_menu_widget_main::calculate_position(
submenu_wid.get(), ctx, anchor_x, anchor_y, direction);

submenu_wid->direction = direction;
submenu_wid->x->reset_to(x / ctx.rt.dpi_scale);
submenu_wid->y->reset_to(y / ctx.rt.dpi_scale);

submenu_wid->reset_animation(direction == popup_direction::top_left ||
direction == popup_direction::top_right);
auto parent_menu = parent->downcast<menu_widget>();
if (!parent_menu)
parent_menu = parent->parent->downcast<menu_widget>();
parent_menu->current_submenu = submenu_wid;
parent_menu->rendering_submenus.push_back(submenu_wid);
submenu_wid->parent_menu = parent_menu.get();
show_submenu(ctx);
}
} else {
if (submenu_wid) {
submenu_wid->close();
submenu_wid = nullptr;
}
hide_submenu();
}
} else {
if (submenu_wid) {
submenu_wid->close();
submenu_wid = nullptr;
}
hide_submenu();
}

if (submenu_wid && submenu_wid->dying_time.has_value) {
Expand Down Expand Up @@ -397,7 +365,7 @@ void mb_shell::menu_item_normal_widget::reset_appear_animation(float delay) {
};
opacity->reset_to(0);
this->x->reset_to(-20);

config::current->context_menu.theme.animation.item.opacity(opacity, delay);
config::current->context_menu.theme.animation.item.x(x, delay);
config::current->context_menu.theme.animation.item.width(width);
Expand Down Expand Up @@ -729,3 +697,47 @@ void mb_shell::menu_item_parent_widget::reset_appear_animation(float delay) {
opacity->reset_to(0);
opacity->animate_to(255);
}
void mb_shell::menu_item_normal_widget::hide_submenu() {
if (submenu_wid) {
submenu_wid->close();
submenu_wid = nullptr;
}
}
void mb_shell::menu_item_normal_widget::show_submenu(ui::update_context &ctx) {
if (submenu_wid)
return;
submenu_wid = std::make_shared<menu_widget>();
item.submenu.value()(submenu_wid);

auto anchor_x = *width + *x + ctx.offset_x;
auto anchor_y = **y + ctx.offset_y;

anchor_x *= ctx.rt.dpi_scale;
anchor_y *= ctx.rt.dpi_scale;

submenu_wid->update(ctx);
auto direction = mouse_menu_widget_main::calculate_direction(
submenu_wid.get(), ctx, anchor_x, anchor_y,
popup_direction::bottom_right);

if (direction == popup_direction::top_left ||
direction == popup_direction::bottom_left) {
anchor_x -= *width;
}

auto [x, y] = mouse_menu_widget_main::calculate_position(
submenu_wid.get(), ctx, anchor_x, anchor_y, direction);

submenu_wid->direction = direction;
submenu_wid->x->reset_to(x / ctx.rt.dpi_scale);
submenu_wid->y->reset_to(y / ctx.rt.dpi_scale);

submenu_wid->reset_animation(direction == popup_direction::top_left ||
direction == popup_direction::top_right);
auto parent_menu = parent->downcast<menu_widget>();
if (!parent_menu)
parent_menu = parent->parent->downcast<menu_widget>();
parent_menu->current_submenu = submenu_wid;
parent_menu->rendering_submenus.push_back(submenu_wid);
submenu_wid->parent_menu = parent_menu.get();
}
5 changes: 4 additions & 1 deletion src/shell/contextmenu/menu_widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ struct menu_item_normal_widget : public menu_item_widget {
bool has_icon_padding = false;
float padding = config::current->context_menu.theme.padding;
float icon_padding = config::current->context_menu.theme.icon_padding;
float right_icon_padding = config::current->context_menu.theme.right_icon_padding;
float right_icon_padding =
config::current->context_menu.theme.right_icon_padding;
menu_item_normal_widget(menu_item item);
void reset_appear_animation(float delay) override;

Expand All @@ -52,6 +53,8 @@ struct menu_item_normal_widget : public menu_item_widget {
float measure_width(ui::update_context &ctx) override;
bool check_hit(const ui::update_context &ctx) override;

void hide_submenu();
void show_submenu(ui::update_context &ctx);
void reload_icon_img(ui::nanovg_context ctx);
};

Expand Down

0 comments on commit 9bf7795

Please sign in to comment.