Move Widgets::Base to Curses::React::Component
This commit is contained in:
parent
b8d7391703
commit
f375e2d103
9 changed files with 56 additions and 54 deletions
49
lib/curses/react/component.rb
Normal file
49
lib/curses/react/component.rb
Normal file
|
@ -0,0 +1,49 @@
|
||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module Curses
|
||||||
|
module React
|
||||||
|
class Component
|
||||||
|
def initialize(parent)
|
||||||
|
@parent = parent
|
||||||
|
@props = {}.freeze
|
||||||
|
end
|
||||||
|
|
||||||
|
def props=(value)
|
||||||
|
raise TypeError, "expected props to be a #{Hash}" unless value.is_a? Hash
|
||||||
|
raise ArgumentError, 'expected props to be frozen' unless value.frozen?
|
||||||
|
|
||||||
|
@window = nil if props[:x] != value[:x] || props[:y] != value[:y] ||
|
||||||
|
props[:width] != value[:width] || props[:height] != value[:height]
|
||||||
|
|
||||||
|
@props = value
|
||||||
|
end
|
||||||
|
|
||||||
|
def trigger(event); end
|
||||||
|
|
||||||
|
def render
|
||||||
|
draw
|
||||||
|
window.refresh
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
attr_reader :parent, :props
|
||||||
|
|
||||||
|
def window
|
||||||
|
@window ||= parent&.subwin(props[:x], props[:y], props[:width], props[:height]) || Curses.stdscr
|
||||||
|
end
|
||||||
|
|
||||||
|
def draw
|
||||||
|
raise NotImplementedError, "#{self.class}#draw"
|
||||||
|
end
|
||||||
|
|
||||||
|
def setpos(x, y)
|
||||||
|
window.setpos y, x
|
||||||
|
end
|
||||||
|
|
||||||
|
def addstr(s)
|
||||||
|
window.addstr s
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -1,13 +1,13 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
require 'curses'
|
require 'curses'
|
||||||
|
require 'curses/react/component'
|
||||||
|
|
||||||
# Additional classes
|
# Additional classes
|
||||||
require 'events'
|
require 'events'
|
||||||
require 'style'
|
require 'style'
|
||||||
|
|
||||||
# Basic
|
# Basic
|
||||||
require 'widgets/base'
|
|
||||||
require 'widgets/text'
|
require 'widgets/text'
|
||||||
|
|
||||||
# Basic containers
|
# Basic containers
|
||||||
|
|
|
@ -1,47 +0,0 @@
|
||||||
# frozen_string_literal: true
|
|
||||||
|
|
||||||
module Widgets
|
|
||||||
class Base
|
|
||||||
def initialize(parent)
|
|
||||||
@parent = parent
|
|
||||||
@props = {}.freeze
|
|
||||||
end
|
|
||||||
|
|
||||||
def props=(value)
|
|
||||||
raise TypeError, "expected props to be a #{Hash}" unless value.is_a? Hash
|
|
||||||
raise ArgumentError, 'expected props to be frozen' unless value.frozen?
|
|
||||||
|
|
||||||
@window = nil if props[:x] != value[:x] || props[:y] != value[:y] ||
|
|
||||||
props[:width] != value[:width] || props[:height] != value[:height]
|
|
||||||
|
|
||||||
@props = value
|
|
||||||
end
|
|
||||||
|
|
||||||
def trigger(event); end
|
|
||||||
|
|
||||||
def render
|
|
||||||
draw
|
|
||||||
window.refresh
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
|
||||||
|
|
||||||
attr_reader :parent, :props
|
|
||||||
|
|
||||||
def window
|
|
||||||
@window ||= parent&.subwin(props[:x], props[:y], props[:width], props[:height]) || Curses.stdscr
|
|
||||||
end
|
|
||||||
|
|
||||||
def draw
|
|
||||||
raise NotImplementedError, "#{self.class}#draw"
|
|
||||||
end
|
|
||||||
|
|
||||||
def setpos(x, y)
|
|
||||||
window.setpos y, x
|
|
||||||
end
|
|
||||||
|
|
||||||
def addstr(s)
|
|
||||||
window.addstr s
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
module Widgets
|
module Widgets
|
||||||
class Chat < VPanel
|
class Chat < VPanel
|
||||||
class History < Base
|
class History < Curses::React::Component
|
||||||
private
|
private
|
||||||
|
|
||||||
def draw
|
def draw
|
||||||
|
|
|
@ -4,7 +4,7 @@ module Widgets
|
||||||
using Helpers
|
using Helpers
|
||||||
|
|
||||||
class Chat < VPanel
|
class Chat < VPanel
|
||||||
class Info < Base
|
class Info < Curses::React::Component
|
||||||
PUBLIC_KEY_LABEL = 'Public key: '
|
PUBLIC_KEY_LABEL = 'Public key: '
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Widgets
|
module Widgets
|
||||||
class Container < Base
|
class Container < Curses::React::Component
|
||||||
def subwin(x, y, width, height)
|
def subwin(x, y, width, height)
|
||||||
window.subwin height, width, y, x
|
window.subwin height, width, y, x
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Widgets
|
module Widgets
|
||||||
class Logo < Base
|
class Logo < Curses::React::Component
|
||||||
LOGO = [
|
LOGO = [
|
||||||
' _____ ___ _ _ ___ _ _ ',
|
' _____ ___ _ _ ___ _ _ ',
|
||||||
' |_ _/ _ \ \/ / _ \| \ | | ',
|
' |_ _/ _ \ \/ / _ \| \ | | ',
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
module Widgets
|
module Widgets
|
||||||
using Helpers
|
using Helpers
|
||||||
|
|
||||||
class Menu < Base
|
class Menu < Curses::React::Component
|
||||||
def trigger(event)
|
def trigger(event)
|
||||||
case event
|
case event
|
||||||
when Events::Text::Up
|
when Events::Text::Up
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
module Widgets
|
module Widgets
|
||||||
class Text < Base
|
class Text < Curses::React::Component
|
||||||
def trigger(event)
|
def trigger(event)
|
||||||
case event
|
case event
|
||||||
when Events::Text::Enter
|
when Events::Text::Enter
|
||||||
|
|
Reference in a new issue