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

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 module Widgets
class Chat < VPanel class Chat < VPanel
class History < Base class History < Curses::React::Component
private private
def draw def draw

View file

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

View file

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

View file

@ -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 = [
' _____ ___ _ _ ___ _ _ ', ' _____ ___ _ _ ___ _ _ ',
' |_ _/ _ \ \/ / _ \| \ | | ', ' |_ _/ _ \ \/ / _ \| \ | | ',

View file

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

View file

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