From 32643aba1797aca10570eee84b9336bf003053f3 Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Fri, 21 Jul 2017 17:36:20 +0000 Subject: [PATCH] Implement focus for messenger --- .rubocop.yml | 3 +++ lib/main.rb | 2 ++ lib/widgets/chat.rb | 5 +++++ lib/widgets/list.rb | 5 ++++- lib/widgets/messenger.rb | 32 +++++++++++++++++++++++++++++++- lib/widgets/peers.rb | 10 ++++++++++ lib/widgets/search.rb | 4 +++- 7 files changed, 58 insertions(+), 3 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 5a31f0b..69c2fb9 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -11,6 +11,9 @@ Metrics/LineLength: Style/Documentation: Enabled: false +Style/DoubleNegation: + Enabled: false + Style/TrailingCommaInArguments: EnforcedStyleForMultiline: comma diff --git a/lib/main.rb b/lib/main.rb index 6412889..2d39e9f 100644 --- a/lib/main.rb +++ b/lib/main.rb @@ -88,6 +88,8 @@ private @menu = Widgets::Menu.new menu_left, 0, menu_width, Curses.stdscr.maxy @messenger = Widgets::Messenger.new messenger_left, 0, messenger_width, Curses.stdscr.maxy + + @messenger.focused = true end def render diff --git a/lib/widgets/chat.rb b/lib/widgets/chat.rb index 36fcfce..e0177f9 100644 --- a/lib/widgets/chat.rb +++ b/lib/widgets/chat.rb @@ -3,6 +3,7 @@ module Widgets class Chat attr_reader :x, :y, :width, :height + attr_accessor :focused attr_reader :messages def initialize(x, y, width, height) @@ -12,6 +13,8 @@ module Widgets @width = width @height = height + @focused = false + @messages = 1.upto(100).map do { time: Faker::Time.forward, @@ -64,5 +67,7 @@ module Widgets offset + 1 + lines end + + def trigger(event); end end end diff --git a/lib/widgets/list.rb b/lib/widgets/list.rb index 590e45b..6e4f287 100644 --- a/lib/widgets/list.rb +++ b/lib/widgets/list.rb @@ -3,6 +3,7 @@ module Widgets class List attr_reader :x, :y, :width, :height, :active, :top, :items + attr_accessor :focused def initialize(x, y, width, height) @x = x @@ -14,6 +15,8 @@ module Widgets @active = 0 @top = 0 + @focused = false + @items = 1.upto(height - 1 + 10).map do Faker::Name.name end @@ -23,7 +26,7 @@ module Widgets items[top...(top + height)].each_with_index.each do |item, offset| index = top + offset - if index == active + if index == active && focused Curses.attron Curses.color_pair 2 else Curses.attron Curses.color_pair 1 diff --git a/lib/widgets/messenger.rb b/lib/widgets/messenger.rb index 01ba795..3cf4a4a 100644 --- a/lib/widgets/messenger.rb +++ b/lib/widgets/messenger.rb @@ -3,6 +3,7 @@ module Widgets class Messenger attr_reader :x, :y, :width, :height + attr_reader :focused, :focus def initialize(x, y, width, height) @x = x @@ -19,6 +20,9 @@ module Widgets @peers = Widgets::Peers.new x + peers_left, y, peers_width, height @chat = Widgets::Chat.new x + chat_left, y, chat_width, height + + @focused = false + @focus = @peers end def render @@ -27,7 +31,33 @@ module Widgets end def trigger(event) - @peers.trigger event + case event + when Events::Window::Left + left + when Events::Window::Right + right + else + @focus.trigger event + end + end + + def left + self.focus = @peers + end + + def right + self.focus = @chat + end + + def focused=(value) + @focused = !!value + focus.focused = focused + end + + def focus=(child) + focus.focused = false + @focus = child + focus.focused = true if focused end end end diff --git a/lib/widgets/peers.rb b/lib/widgets/peers.rb index 83bcc9e..e53965a 100644 --- a/lib/widgets/peers.rb +++ b/lib/widgets/peers.rb @@ -2,7 +2,11 @@ module Widgets class Peers + attr_reader :focused + def initialize(x, y, width, height) + @focused = false + @search = Widgets::Search.new x, y, width, 1 @list = Widgets::List.new x, y + 1, width, height - 1 end @@ -20,5 +24,11 @@ module Widgets @search.trigger event end end + + def focused=(value) + @focused = !!value + @list.focused = focused + @search.focused = focused + end end end diff --git a/lib/widgets/search.rb b/lib/widgets/search.rb index b7be603..0510488 100644 --- a/lib/widgets/search.rb +++ b/lib/widgets/search.rb @@ -3,6 +3,7 @@ module Widgets class Search attr_reader :x, :y, :width, :height, :text, :cursor_pos + attr_accessor :focused def initialize(x, y, width, height) @x = x @@ -11,6 +12,7 @@ module Widgets @height = height @text = '' @cursor_pos = 0 + @focused = false end def render @@ -28,7 +30,7 @@ module Widgets Curses.attron Curses.color_pair 3 Curses.addstr before_cursor - Curses.attron Curses.color_pair 4 + Curses.attron Curses.color_pair 4 if focused Curses.addstr under_cursor Curses.attron Curses.color_pair 3