diff --git a/lib/main.rb b/lib/main.rb index 3fa8f7a..78fc00d 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -63,9 +63,12 @@ private focus: :sidebar, focused: true, - on_window_left: method(:on_window_left), + on_window_left: method(:on_window_left), on_window_right: method(:on_window_right), + on_menu_up: method(:on_menu_up), + on_menu_down: method(:on_menu_down), + sidebar: { x: 0, y: 0, @@ -190,4 +193,56 @@ private ).freeze, ).freeze end + + def on_menu_up + active = state[:sidebar][:menu][:active] + top = state[:sidebar][:menu][:top] + items = state[:sidebar][:menu][:items] + height = state[:sidebar][:menu][:height] + + active -= 1 + + active = items.count - 1 if active.negative? + + if active < top + top = active + elsif active >= top + height + top = active - height + 1 + end + + @state = state.merge( + sidebar: state[:sidebar].merge( + menu: state[:sidebar][:menu].merge( + active: active, + top: top, + ).freeze, + ).freeze, + ).freeze + end + + def on_menu_down + active = state[:sidebar][:menu][:active] + top = state[:sidebar][:menu][:top] + items = state[:sidebar][:menu][:items] + height = state[:sidebar][:menu][:height] + + active += 1 + + active = 0 if active >= items.count + + if active < top + top = active + elsif active >= top + height + top = active - height + 1 + end + + @state = state.merge( + sidebar: state[:sidebar].merge( + menu: state[:sidebar][:menu].merge( + active: active, + top: top, + ).freeze, + ).freeze, + ).freeze + end end diff --git a/lib/widgets/main.rb b/lib/widgets/main.rb index 8eeac03..a64e301 100644 --- a/lib/widgets/main.rb +++ b/lib/widgets/main.rb @@ -11,8 +11,13 @@ module Widgets def props=(_value) super - @sidebar.props = props[:sidebar] - @chat.props = props[:chat] + + @sidebar.props = props[:sidebar].merge( + on_menu_up: props[:on_menu_up], + on_menu_down: props[:on_menu_down], + ).freeze + + @chat.props = props[:chat] end def trigger(event) diff --git a/lib/widgets/menu.rb b/lib/widgets/menu.rb index 6b05b81..b44df0b 100644 --- a/lib/widgets/menu.rb +++ b/lib/widgets/menu.rb @@ -2,6 +2,15 @@ module Widgets class Menu < Base + def trigger(event) + case event + when Events::Text::Up + props[:on_up].call + when Events::Text::Down + props[:on_down].call + end + end + private def draw diff --git a/lib/widgets/sidebar.rb b/lib/widgets/sidebar.rb index c61d882..b8c72b8 100644 --- a/lib/widgets/sidebar.rb +++ b/lib/widgets/sidebar.rb @@ -11,8 +11,17 @@ module Widgets def props=(_value) super + @logo.props = props[:logo] - @menu.props = props[:menu] + + @menu.props = props[:menu].merge( + on_up: props[:on_menu_up], + on_down: props[:on_menu_down], + ).freeze + end + + def trigger(event) + focus&.trigger event end private