Archived
1
0
Fork 0

Handle menu navigation

This commit is contained in:
Braiden Vasco 2017-07-25 16:22:38 +00:00
parent b9c6dca1cd
commit 7e01c6febd
4 changed files with 82 additions and 4 deletions

View file

@ -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

View file

@ -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)

View file

@ -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

View file

@ -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