Skip to content

Commit

Permalink
Change how TextDrawables initialize and work, and which methods are i…
Browse files Browse the repository at this point in the history
…nherited where.

Add extra TextDrawables like del, ins, sub, sup.
Bring TextDrawable up to date with default values, etc.
More tests, and replacing some old ones with the new interface.
  • Loading branch information
noahgibbs committed Dec 19, 2023
1 parent ea03086 commit fc46d37
Show file tree
Hide file tree
Showing 36 changed files with 763 additions and 365 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ still making big changes.
### Enhancements

- Ovals!
- Lots more text methods: del, sub, sup; lots more text styles: underline, strikethrough, strikecolor, align
- Features! Shoes.app(feature: [:html, :scarpe]) lets apps declare dependencies on non-classic Shoes!
- Better handling of :left, :top, :width and :height, :margin and :padding on more drawables
- The html_class style is a feature to make it easier to do Bootstrap styling on your drawables
- Directly run Shoes Specs, including with Niente
- We use Minitest assertion DSL rather than our own everywhere now
- Better handling of :left, :top, :width and :height, :margin and :padding on more drawables

### Bugs Fixed

Expand All @@ -36,6 +37,7 @@ still making big changes.

### Incompatibilities

TextDrawables now draw with very different Calzini (HTML renderer) properties
We're deprecating the CatsCradle test DSL in favour of Shoes-Spec.
Some error names have changed, with more to come.
We've changed the Lacci drawable-create event to include the parent ID.
Expand Down
6 changes: 4 additions & 2 deletions examples/span.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Shoes.app :height => 500, :width => 500 do
stack :margin => 10 do
para span("TEXT EDITOR", :stroke => "blue", :fill => "green"), " * USE ALT-Q TO QUIT", :stroke => "red"
para span("TEXT EDITOR", :stroke => blue, :fill => green), " * USE ALT-Q TO QUIT", :stroke => red
end
span ("text")
para "Various ", del("text"), " in ", sub("various"), " ", sup("styles"), " can be ", ins("hard to read"), "...\n"

para "A ", span("wide", underline: "single", undercolor: blue), " ", span("variety", underline: "error", undercolor: green), " ", span("of", underline: "double"), " ", span("underlines", underline: "low", undercolor: darkgreen)
end
1 change: 0 additions & 1 deletion lacci/lib/shoes/drawables.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,5 @@
require "shoes/drawables/list_box"
require "shoes/drawables/para"
require "shoes/drawables/radio"
require "shoes/drawables/span"
require "shoes/drawables/video"
require "shoes/drawables/progress"
2 changes: 1 addition & 1 deletion lacci/lib/shoes/drawables/edit_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def change(&block)
end

def append(new_text)
self.text = self.text + new_text
self.text = (self.text || "") + new_text
end
end
end
10 changes: 4 additions & 6 deletions lacci/lib/shoes/drawables/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ class Link < Shoes::TextDrawable
shoes_styles :text, :click, :has_block
shoes_events :click

Shoes::Drawable.drawable_default_styles[Shoes::Link][:click] = "#"
#Shoes::Drawable.drawable_default_styles[Shoes::Link][:click] = "#"

init_args :text
def initialize(text, click: nil, &block)
init_args # Empty by the time it reaches Drawable#initialize
def initialize(*args, **kwargs, &block)
@block = block
# We can't send a block to the display drawable, but we can send a boolean
@has_block = !block.nil?
Expand All @@ -18,16 +18,14 @@ def initialize(text, click: nil, &block)
bind_self_event("click") do
@block&.call
end

create_display_drawable
end
end

# In Shoes, the LinkHover pseudo-class is used to set default styles for links when
# hovered over. The functionality isn't present in Lacci yet.
class LinkHover < Link
def initialize
raise "This class should never be instantiated! Use link, not link_hover!"
raise "This class should never be instantiated directly! Use link, not link_hover!"
end
end
end
7 changes: 3 additions & 4 deletions lacci/lib/shoes/drawables/para.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
class Shoes
class Para < Shoes::Drawable
shoes_styles :text_items, :size, :font
shoes_style(:stroke) { |val| Shoes::Colors.to_rgb(val) }
shoes_style(:stroke) { |val, _name| Shoes::Colors.to_rgb(val) }
shoes_style(:fill) { |val, _name| Shoes::Colors.to_rgb(val) }

shoes_style(:align) do |val|
unless ["left", "center", "right"].include?(val)
Expand Down Expand Up @@ -51,7 +52,7 @@ def initialize(*args, **kwargs)
private

def text_children_to_items(text_children)
text_children.map { |arg| arg.is_a?(String) ? arg : arg.linkable_id }
text_children.map { |arg| arg.is_a?(TextDrawable) ? arg.linkable_id : arg.to_s }
end

public
Expand Down Expand Up @@ -159,7 +160,5 @@ def caption(*args, **kwargs)
def inscription(*args, **kwargs)
para(*args, **{ size: :inscription }.merge(kwargs))
end

alias_method :ins, :inscription
end
end
27 changes: 0 additions & 27 deletions lacci/lib/shoes/drawables/span.rb

This file was deleted.

121 changes: 87 additions & 34 deletions lacci/lib/shoes/drawables/text_drawable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,61 +9,114 @@ class Shoes
# have methods app, contents, children, parent,
# style, to_s, text, text= and replace.
#
# We don't currently allow things like em("oh", strong("hi!")),
# so we'll need a rework to match the old interface at
# some point.
# Much of what this does and how is similar to Para.
# It's a very similar API.
class TextDrawable < Shoes::Drawable
class << self
# rubocop:disable Lint/MissingSuper
def inherited(subclass)
Shoes::Drawable.drawable_classes ||= []
Shoes::Drawable.drawable_classes << subclass

Shoes::Drawable.drawable_default_styles ||= {}
Shoes::Drawable.drawable_default_styles[subclass] = {}
shoes_styles :text_items, :size, :stroke, :strokewidth, :fill, :undercolor, :font

STRIKETHROUGH_VALUES = [nil, "none", "single"]
shoes_style :strikethrough do |val, _name|
unless STRIKETHROUGH_VALUES.include?(val)
raise Shoes::Errors::InvalidAttributeValueError, "Strikethrough must be one of: #{STRIKETHROUGH_VALUES.inspect}!"
end
val
end

UNDERLINE_VALUES = [nil, "none", "single", "double", "low", "error"]
shoes_style :underline do |val, _name|
unless UNDERLINE_VALUES.include?(val)
raise Shoes::Errors::InvalidAttributeValueError, "Underline must be one of: #{UNDERLINE_VALUES.inspect}!"
end
# rubocop:enable Lint/MissingSuper
val
end

shoes_events # No TextDrawable-specific events yet

def initialize(*args, **kwargs)
# Don't pass text_children args to Drawable#initialize
super(*[], **kwargs)

# Text_children alternates strings and TextDrawables, so we can't just pass
# it as a Shoes style. It won't serialize.
update_text_children(args)

create_display_drawable
end

def text_children_to_items(text_children)
text_children.map { |arg| arg.is_a?(TextDrawable) ? arg.linkable_id : arg.to_s }
end

# Sets the paragraph text to a new value, which can
# include {TextDrawable}s like em(), strong(), etc.
#
# @param children [Array] the arguments can be Strings and/or TextDrawables
# @return [void]
def replace(*children)
update_text_children(children)
end

# Set the paragraph text to a single String.
# To use bold, italics, etc. use {Para#replace} instead.
#
# @param child [String] the new text to use for this Para
# @return [void]
def text=(*children)
update_text_children(children)
end

# Return the text, but not the styling, of the para's
# contents. For example, if the contents had strong
# and emphasized text, the bold and emphasized would
# be removed but the text would be returned.
#
# @return [String] the text from this para
def text
@text_children.map(&:to_s).join
end

# Return the text but not styling from the para. This
# is the same as #text.
#
# @return [String] the text from this para
def to_s
self.text
end

private

# Text_children alternates strings and TextDrawables, so we can't just pass
# it as a Shoes style. It won't serialize.
def update_text_children(children)
@text_children = children.flatten
# This should signal the display drawable to change
self.text_items = text_children_to_items(@text_children)
end
end

class << self
def default_text_drawable_with(element)
class_name = element.capitalize

drawable_class = Class.new(Shoes::TextDrawable) do
shoes_style :content
shoes_events # No specific events

init_args # We're going to pass an empty array to super
def initialize(content)
super()

@content = content

create_display_drawable
end

def text
self.content
end

def to_s
self.content
end

def text=(new_text)
self.content = new_text
end
end
Shoes.const_set class_name, drawable_class
end
end
end

# Shoes3 subclasses of cText were: code, del, em, ins, span, strong, sup, sub

Shoes.default_text_drawable_with(:code)
Shoes.default_text_drawable_with(:del)
Shoes.default_text_drawable_with(:em)
Shoes.default_text_drawable_with(:strong)
Shoes.default_text_drawable_with(:span)
Shoes.default_text_drawable_with(:sub)
Shoes.default_text_drawable_with(:sup)
Shoes.default_text_drawable_with(:ins) # in Shoes3, looks like "ins" is just underline

# Defaults must come *after* classes are defined

Shoes::Drawable.drawable_default_styles[Shoes::Ins][:underline] = "single"
1 change: 0 additions & 1 deletion lib/scarpe/wv.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ class Scarpe::Webview::Drawable < Shoes::Linkable
require_relative "wv/shape"

require_relative "wv/text_drawable"
require_relative "wv/span"
require_relative "wv/link"
require_relative "wv/line"
require_relative "wv/rect"
Expand Down
4 changes: 2 additions & 2 deletions lib/scarpe/wv/document_root.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def initialize(properties)
when "font"
@fonts << args[0]
# Can't just create font_updater and alert_updater on initialize - not everything is set up
@font_updater ||= Scarpe::Webview::WebWrangler::ElementWrangler.new("root-fonts")
@font_updater ||= Scarpe::Webview::WebWrangler::ElementWrangler.new(html_id: "root-fonts")
@font_updater.inner_html = font_contents
when "alert"
bind_ok_event
@alerts << args[0]
@alert_updater ||= Scarpe::Webview::WebWrangler::ElementWrangler.new("root-alerts")
@alert_updater ||= Scarpe::Webview::WebWrangler::ElementWrangler.new(html_id: "root-alerts")
@alert_updater.inner_html = alert_contents
else
raise Scarpe::UnknownBuiltinCommandError, "Unexpected builtin command: #{cmd_name.inspect}!"
Expand Down
2 changes: 1 addition & 1 deletion lib/scarpe/wv/drawable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def add_child(child)
#
# @return [Scarpe::WebWrangler::ElementWrangler] a DOM object manager
def html_element
@elt_wrangler ||= Scarpe::Webview::WebWrangler::ElementWrangler.new(html_id)
@elt_wrangler ||= Scarpe::Webview::WebWrangler::ElementWrangler.new(html_id:)
end

# Return a promise that guarantees all currently-requested changes have completed
Expand Down
6 changes: 4 additions & 2 deletions lib/scarpe/wv/link.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ def initialize(properties)
end
end

def element
render "link"
def to_calzini_hash
h = super
h[:tag] = "a"
h
end
end
end
1 change: 1 addition & 0 deletions lib/scarpe/wv/para.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def to_html
private

def child_markup
# The children should be only text strings or TextDrawables.
items_to_display_children(@text_items).map do |child|
if child.respond_to?(:to_html)
child.to_html
Expand Down
44 changes: 0 additions & 44 deletions lib/scarpe/wv/span.rb

This file was deleted.

Loading

0 comments on commit fc46d37

Please sign in to comment.