Handle menu navigation
This commit is contained in:
parent
b9c6dca1cd
commit
7e01c6febd
4 changed files with 82 additions and 4 deletions
57
lib/main.rb
57
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
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
Reference in a new issue