Archived
1
0
Fork 0

Implement focus for messenger

This commit is contained in:
Braiden Vasco 2017-07-21 17:36:20 +00:00
parent f6c0b1dd87
commit 32643aba17
7 changed files with 58 additions and 3 deletions

View file

@ -11,6 +11,9 @@ Metrics/LineLength:
Style/Documentation:
Enabled: false
Style/DoubleNegation:
Enabled: false
Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma

View file

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

View file

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

View file

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

View file

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

View file

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

View file

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