From 8c6ffb22cb301aee1b497253d0afd510fb7afdae Mon Sep 17 00:00:00 2001 From: Braiden Vasco Date: Sat, 22 Jul 2017 15:37:22 +0000 Subject: [PATCH] Move methods to base class --- lib/widgets/container.rb | 15 +++++++++++++++ lib/widgets/menu.rb | 7 +++---- lib/widgets/menu/items.rb | 2 +- lib/widgets/menu/logo.rb | 2 +- lib/widgets/messenger.rb | 17 ++++------------- lib/widgets/v_panel.rb | 7 ------- lib/widgets/window.rb | 15 ++++----------- 7 files changed, 28 insertions(+), 37 deletions(-) diff --git a/lib/widgets/container.rb b/lib/widgets/container.rb index f041c11..2c6a1f9 100644 --- a/lib/widgets/container.rb +++ b/lib/widgets/container.rb @@ -2,5 +2,20 @@ module Widgets class Container < Base + attr_reader :focus + + def draw + children.each(&:render) + end + + def children + raise NotImplementedError, "#{self.class}#children" + end + + def focus=(value) + focus&.focused = false + @focus = value + focus.focused = true + end end end diff --git a/lib/widgets/menu.rb b/lib/widgets/menu.rb index 3f8a68d..eb6752d 100644 --- a/lib/widgets/menu.rb +++ b/lib/widgets/menu.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Widgets - class Menu < Base + class Menu < Container def initialize(x, y, _width, height) super x, y, Logo::WIDTH, height @@ -9,9 +9,8 @@ module Widgets @items = Items.new x, @logo.height, @logo.width, nil end - def draw - @logo.render - @items.render + def children + [@logo, @items] end end end diff --git a/lib/widgets/menu/items.rb b/lib/widgets/menu/items.rb index 39fd2b5..5bab600 100644 --- a/lib/widgets/menu/items.rb +++ b/lib/widgets/menu/items.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Widgets - class Menu < Base + class Menu < Container class Items < Base ITEMS = [ 'Foo menu item', diff --git a/lib/widgets/menu/logo.rb b/lib/widgets/menu/logo.rb index 588d70b..5e2457e 100644 --- a/lib/widgets/menu/logo.rb +++ b/lib/widgets/menu/logo.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true module Widgets - class Menu < Base + class Menu < Container class Logo < Base LOGO = [ ' _____ ___ _ _ ___ _ _ ', diff --git a/lib/widgets/messenger.rb b/lib/widgets/messenger.rb index 6cfcf9c..fa097a9 100644 --- a/lib/widgets/messenger.rb +++ b/lib/widgets/messenger.rb @@ -2,8 +2,6 @@ module Widgets class Messenger < Container - attr_reader :focus - def initialize(x, y, width, height) super @@ -16,12 +14,11 @@ module Widgets @peers = Widgets::Peers.new x + peers_left, y, peers_width, height @chat = Widgets::Chat.new x + chat_left, y, chat_width, height - @focus = @peers + self.focus = @peers end - def draw - @peers.render - @chat.render + def children + [@peers, @chat] end def trigger(event) @@ -31,7 +28,7 @@ module Widgets when Events::Window::Right right else - @focus.trigger event + focus.trigger event end end @@ -47,11 +44,5 @@ module Widgets @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/v_panel.rb b/lib/widgets/v_panel.rb index 39f2d5e..fcc0f26 100644 --- a/lib/widgets/v_panel.rb +++ b/lib/widgets/v_panel.rb @@ -2,12 +2,5 @@ module Widgets class VPanel < Container - def draw - children.each(&:render) - end - - def children - raise NotImplementedError, "#{self.class}#children" - end end end diff --git a/lib/widgets/window.rb b/lib/widgets/window.rb index 365bf39..c87cda4 100644 --- a/lib/widgets/window.rb +++ b/lib/widgets/window.rb @@ -13,19 +13,12 @@ module Widgets self.focus = @messenger end + def children + [@menu, @messenger] + end + def trigger(event) focus.trigger event end - - def draw - @menu.render - @messenger.render - end - - def focus=(value) - focus.focused = false if focus - @focus = value - focus.focused = true - end end end