Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

starting docs #417

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/examples/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Shoes.app(title: "Shoes", width: 600, height: 600,resizable: false) do
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is it ok to do this? docs/examples, i did this becoz i though it would be easy to show usage of them
i can show examples directory but we "might" change them sometime
but we rarely touch docs folder.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Definitely reasonable to add examples in the docs folder. We should figure out where to link them from. But it's fine to add them before we figure that out.

end
3 changes: 3 additions & 0 deletions docs/examples/background.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Shoes.app(title: "Shoes", width: 600, height: 600,resizable: false) do
background "purple"
end
3 changes: 3 additions & 0 deletions docs/examples/border.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Shoes.app(title: "Shoes", width: 600, height: 600,resizable: false) do
border red, strokewidth: 6000
end
3 changes: 3 additions & 0 deletions docs/examples/editline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Shoes.app do
edit_line "Hello"
end
48 changes: 42 additions & 6 deletions lacci/lib/shoes/app.rb
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
# frozen_string_literal: true

module Shoes
# The main class for creating Shoes applications.
class App < Shoes::Drawable
include Shoes::Log

class << self
attr_accessor :instance
end

# @return [Object] The root of the document for this application.
attr_reader :document_root

shoes_styles :title, :width, :height, :resizable

CUSTOM_EVENT_LOOP_TYPES = ["displaylib", "return", "wait"]

# @param title [String] The title of the application window.
# @param width [Integer] The width of the application window.
# @param height [Integer] The height of the application window.
# @param resizable [Boolean] Set to `true` to allow window resizing.
# @see https://github.com/scarpe-team/scarpe/tree/main/docs/examples/app.rb
def initialize(
title: "Shoes!",
width: 480,
Expand All @@ -23,6 +30,9 @@ def initialize(
)
log_init("Shoes::App")

# Initializes the Shoes application.

# Checks if another Shoes::App instance already exists in the same process.
if Shoes::App.instance
@log.error("Trying to create a second Shoes::App in the same process! Fail!")
raise Scarpe::TooManyInstancesError, "Cannot create multiple Shoes::App objects!"
Expand All @@ -41,6 +51,7 @@ def initialize(
@draw_context = {
"fill" => "",
"stroke" => "",
"rotate" => 0,
}

# This creates the DocumentRoot, including its corresponding display drawable
Expand Down Expand Up @@ -95,7 +106,9 @@ def initialize(
end
end

# Initializes the application.
def init
# Sends the "init" event.
send_shoes_event(event_name: "init")
return if @do_shutdown

Expand All @@ -105,20 +118,26 @@ def init
# "Container" drawables like flows, stacks, masks and the document root
# are considered "slots" in Shoes parlance. When a new slot is created,
# we push it here in order to track what drawables are found in that slot.
# Pushes a new slot onto the stack.
def push_slot(slot)
# Pushes a new slot onto the stack.
@slots.push(slot)
end

# Pops the last slot from the slots stack.
def pop_slot
return if @slots.size <= 1

@slots.pop
end

# Returns the current slot.
# @return [Drawable] The current slot.
def current_slot
@slots[-1]
end

# Executes a block within a specific slot.
def with_slot(slot_item, &block)
return unless block_given?

Expand All @@ -128,6 +147,8 @@ def with_slot(slot_item, &block)
pop_slot
end

# Returns a copy of the current drawing context.
# @return [Hash] The current drawing context.
def current_draw_context
@draw_context.dup
end
Expand Down Expand Up @@ -161,11 +182,15 @@ def run
end
end

# Signals the application to destroy itself.
# @param send_event [Boolean] Set to `true` to send a "destroy" event.
def destroy(send_event: true)
@do_shutdown = true
send_shoes_event(event_name: "destroy") if send_event
end

# Retrieves all drawables within the application.
# @return [Array<Drawable>] An array of all drawables in the application.
def all_drawables
out = []

Expand All @@ -180,6 +205,8 @@ def all_drawables

# We can add various ways to find drawables here.
# These are sort of like Shoes selectors, used for testing.
# @param specs [Array] An array of criteria for finding drawables.
# @return [Array<Drawable>] An array of drawables that match the criteria.
def find_drawables_by(*specs)
drawables = all_drawables
specs.each do |spec|
Expand Down Expand Up @@ -215,43 +242,45 @@ def find_drawables_by(*specs)

# DSL methods
class Shoes::App
# Sets the background of the current slot.
def background(...)
current_slot.background(...)
end

# Sets the border of the current slot.
def border(...)
current_slot.border(...)
end

# Event handler objects

# Defines event handler methods for various events.
events = [:motion, :hover, :leave, :click, :release, :keypress, :animate, :every, :timer]
events.each do |event|
define_method(event) do |*args, &block|
subscription_item(args:, shoes_api_name: event.to_s, &block)
end
end

# Draw context methods

# Sets the fill color in the drawing context.
def fill(color)
@draw_context["fill"] = color
end

# Clears the fill color in the drawing context.
def nofill
@draw_context["fill"] = ""
end

# Sets the stroke color in the drawing context.
def stroke(color)
@draw_context["stroke"] = color
end

# Clears the stroke color in the drawing context.
def nostroke
@draw_context["stroke"] = ""
end

# Shape DSL methods

# Moves the current slot to the specified coordinates.
def move_to(x, y)
raise(InvalidAttributeValueError, "Pass only Numeric arguments to move_to!") unless x.is_a?(Numeric) && y.is_a?(Numeric)

Expand All @@ -260,6 +289,7 @@ def move_to(x, y)
end
end

# Draws a line from the current position to the specified coordinates.
def line_to(x, y)
raise(InvalidAttributeValueError, "Pass only Numeric arguments to line_to!") unless x.is_a?(Numeric) && y.is_a?(Numeric)

Expand All @@ -268,7 +298,13 @@ def line_to(x, y)
end
end

# Rotates the current slot by the specified angle.
def rotate(angle)
@draw_context["rotate"] = angle
end

# Not implemented yet: curve_to, arc_to

# Outputs information to the console.
alias_method :info, :puts
end
13 changes: 12 additions & 1 deletion lacci/lib/shoes/background.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
# frozen_string_literal: true

module Shoes
# The `Background` module provides functionality for setting the background color
module Background
# @param includer [Class] The class including this module.
def self.included(includer)
includer.shoes_style(:background_color)
end

# NOTE: this needs to be passed through in order for the styling to work
# Set the background color of the object to the specified color.
#
# @param color [String] The background color to set. This should be a valid color representation, such as a color name (e.g., "red") or a hex code (e.g., "#FF0000").
# @param options [Hash] (optional) Additional options for setting the background.
# @option options [Any] :option_name A description of an optional parameter.
#
# @return [void]
#
# @see https://github.com/scarpe-team/scarpe/tree/main/docs/examples/background.rb
# NOTE: This method needs to be called in order for the styling to work properly.
def background(color, options = {})
self.background_color = color
end
Expand Down
13 changes: 11 additions & 2 deletions lacci/lib/shoes/border.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
# frozen_string_literal: true

module Shoes
# The `Border` module provides methods for defining border styles in a Shoes-based application.
module Border
# This class method is automatically invoked when the `Border` module is included in another class or module.
# It sets up the styles for border color and options.
#
# @param includer [Class] The class or module that includes the `Border` module.
def self.included(includer)
includer.shoes_styles :border_color, :options
end

# Considering a signature like this:
# border "#00D0FF", :strokewidth => 3, :curve => 12
# Set the border style for an element.
#
# @param color [String] The color of the border.
# @param options [Hash] (optional) A hash of options for customizing the border style.
#
# @see https://github.com/scarpe-team/scarpe/tree/main/docs/examples/border.rb
def border(color, options = {})
self.border_color = color
self.options = options
Expand Down
16 changes: 16 additions & 0 deletions lacci/lib/shoes/drawables/edit_line.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,38 @@
# frozen_string_literal: true

module Shoes
# The `EditLine` class represents an editable text input field in a Shoes application.
# @see https://github.com/scarpe-team/scarpe/tree/main/docs/examples/editline.rb

class EditLine < Shoes::Drawable
# Sets the styles for text content and width.
shoes_styles :text, :width

# Initializes a new `EditLine` instance.
#
# @param text [String] The initial text content of the EditLine.
# @param width [Integer] (optional) The width of the EditLine in pixels.
# @yield [new_text] Optional block to be executed when the text content changes.
# @yieldparam new_text [String] The new text content of the EditLine.
def initialize(text = "", width: nil, &block)
super
@block = block
@text = text

# Binds a "change" event to the EditLine, which triggers when the text content changes.
bind_self_event("change") do |new_text|
self.text = new_text
@block&.call(new_text)
end

# Creates the display drawable for the EditLine.
create_display_drawable
end

# Sets a block to be executed when the text content changes.
#
# @yield [new_text] Block to be executed when the text content changes.
# @yieldparam new_text [String] The new text content of the EditLine.
def change(&block)
@block = block
end
Expand Down
Loading