Move class Widgets::List to Widgets::Peers:List
This commit is contained in:
parent
be3c1fe23b
commit
3612081d64
4 changed files with 85 additions and 83 deletions
|
@ -15,7 +15,7 @@ require 'widgets/messenger'
|
|||
require 'widgets/menu'
|
||||
|
||||
require 'widgets/peers'
|
||||
require 'widgets/list'
|
||||
require 'widgets/peers/list'
|
||||
require 'widgets/search'
|
||||
|
||||
require 'widgets/chat'
|
||||
|
|
|
@ -1,81 +0,0 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Widgets
|
||||
class List
|
||||
attr_reader :x, :y, :width, :height
|
||||
attr_accessor :focused
|
||||
|
||||
attr_reader :active, :top, :items
|
||||
|
||||
def initialize(x, y, width, height)
|
||||
@x = x
|
||||
@y = y
|
||||
|
||||
@width = width
|
||||
@height = height
|
||||
|
||||
@focused = false
|
||||
|
||||
@active = 0
|
||||
@top = 0
|
||||
|
||||
@items = 1.upto(height - 1 + 10).map do
|
||||
Faker::Name.name
|
||||
end
|
||||
end
|
||||
|
||||
def render
|
||||
items[top...(top + height)].each_with_index.each do |item, offset|
|
||||
index = top + offset
|
||||
|
||||
if index == active && focused
|
||||
Curses.attron Curses.color_pair 2
|
||||
else
|
||||
Curses.attron Curses.color_pair 1
|
||||
end
|
||||
|
||||
Curses.setpos y + offset, x
|
||||
|
||||
s = "#{index}: #{item}"
|
||||
if s.length <= width
|
||||
Curses.addstr s.ljust width
|
||||
else
|
||||
Curses.addstr "#{s[0...width - 3]}..."
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def trigger(event)
|
||||
case event
|
||||
when Events::Panel::Up
|
||||
up
|
||||
when Events::Panel::Down
|
||||
down
|
||||
end
|
||||
end
|
||||
|
||||
def up
|
||||
@active -= 1
|
||||
update
|
||||
end
|
||||
|
||||
def down
|
||||
@active += 1
|
||||
update
|
||||
end
|
||||
|
||||
def update
|
||||
if active.negative?
|
||||
@active = items.count - 1
|
||||
elsif active >= items.count
|
||||
@active = 0
|
||||
end
|
||||
|
||||
if active < top
|
||||
@top = active
|
||||
elsif active >= top + height
|
||||
@top = active - height + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -8,7 +8,7 @@ module Widgets
|
|||
@focused = false
|
||||
|
||||
@search = Widgets::Search.new x, y, width, 1
|
||||
@list = Widgets::List.new x, y + 1, width, height - 1
|
||||
@list = List.new x, y + 1, width, height - 1
|
||||
end
|
||||
|
||||
def render
|
||||
|
|
83
lib/widgets/peers/list.rb
Normal file
83
lib/widgets/peers/list.rb
Normal file
|
@ -0,0 +1,83 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
module Widgets
|
||||
class Peers
|
||||
class List
|
||||
attr_reader :x, :y, :width, :height
|
||||
attr_accessor :focused
|
||||
|
||||
attr_reader :active, :top, :items
|
||||
|
||||
def initialize(x, y, width, height)
|
||||
@x = x
|
||||
@y = y
|
||||
|
||||
@width = width
|
||||
@height = height
|
||||
|
||||
@focused = false
|
||||
|
||||
@active = 0
|
||||
@top = 0
|
||||
|
||||
@items = 1.upto(height - 1 + 10).map do
|
||||
Faker::Name.name
|
||||
end
|
||||
end
|
||||
|
||||
def render
|
||||
items[top...(top + height)].each_with_index.each do |item, offset|
|
||||
index = top + offset
|
||||
|
||||
if index == active && focused
|
||||
Curses.attron Curses.color_pair 2
|
||||
else
|
||||
Curses.attron Curses.color_pair 1
|
||||
end
|
||||
|
||||
Curses.setpos y + offset, x
|
||||
|
||||
s = "#{index}: #{item}"
|
||||
if s.length <= width
|
||||
Curses.addstr s.ljust width
|
||||
else
|
||||
Curses.addstr "#{s[0...width - 3]}..."
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def trigger(event)
|
||||
case event
|
||||
when Events::Panel::Up
|
||||
up
|
||||
when Events::Panel::Down
|
||||
down
|
||||
end
|
||||
end
|
||||
|
||||
def up
|
||||
@active -= 1
|
||||
update
|
||||
end
|
||||
|
||||
def down
|
||||
@active += 1
|
||||
update
|
||||
end
|
||||
|
||||
def update
|
||||
if active.negative?
|
||||
@active = items.count - 1
|
||||
elsif active >= items.count
|
||||
@active = 0
|
||||
end
|
||||
|
||||
if active < top
|
||||
@top = active
|
||||
elsif active >= top + height
|
||||
@top = active - height + 1
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Reference in a new issue