Archived
1
0
Fork 0

Move Widgets::Base to Curses::React::Component

This commit is contained in:
Braiden Vasco 2017-07-27 23:47:48 +00:00
parent b8d7391703
commit f375e2d103
9 changed files with 56 additions and 54 deletions

View 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

View file

@ -1,13 +1,13 @@
# frozen_string_literal: true
require 'curses'
require 'curses/react/component'
# Additional classes
require 'events'
require 'style'
# Basic
require 'widgets/base'
require 'widgets/text'
# Basic containers

View file

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

View file

@ -2,7 +2,7 @@
module Widgets
class Chat < VPanel
class History < Base
class History < Curses::React::Component
private
def draw

View file

@ -4,7 +4,7 @@ module Widgets
using Helpers
class Chat < VPanel
class Info < Base
class Info < Curses::React::Component
PUBLIC_KEY_LABEL = 'Public key: '
private

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
module Widgets
class Container < Base
class Container < Curses::React::Component
def subwin(x, y, width, height)
window.subwin height, width, y, x
end

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
module Widgets
class Logo < Base
class Logo < Curses::React::Component
LOGO = [
' _____ ___ _ _ ___ _ _ ',
' |_ _/ _ \ \/ / _ \| \ | | ',

View file

@ -3,7 +3,7 @@
module Widgets
using Helpers
class Menu < Base
class Menu < Curses::React::Component
def trigger(event)
case event
when Events::Text::Up

View file

@ -1,7 +1,7 @@
# frozen_string_literal: true
module Widgets
class Text < Base
class Text < Curses::React::Component
def trigger(event)
case event
when Events::Text::Enter