Skip to content

Commit

Permalink
Started adding documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Qwerp-Derp committed Jul 10, 2018
1 parent 6d0d98f commit 9946822
Show file tree
Hide file tree
Showing 38 changed files with 215 additions and 122 deletions.
12 changes: 5 additions & 7 deletions examples/control_gallery.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ class ControlGallery < Hedron::Application
@mainwin : Hedron::Window?

@progressbar : Hedron::ProgressBar?
@slider : Hedron::Slider?
@spinbox : Hedron::Spinbox?
@slider : Hedron::Slider?
@spinbox : Hedron::Spinbox?

def on_closing(this)
this.destroy
Expand Down Expand Up @@ -52,13 +52,11 @@ class ControlGallery < Hedron::Application
@progressbar.not_nil!.value = value
end

def initialize
super

def draw
file_menu = Hedron::Menu.new("File")
open = Hedron::MenuItem.new(file_menu, "Open")
save = Hedron::MenuItem.new(file_menu, "Save")

file_menu.add_quit
self.on_stop = ->should_quit

Expand Down Expand Up @@ -106,7 +104,7 @@ class ControlGallery < Hedron::Application

entry = Hedron::Entry.new
entry.text = "Entry"

inner.add_all(
entry,
Hedron::Label.new("Label"),
Expand Down
5 changes: 5 additions & 0 deletions examples/ml_gallery.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require "./ml_gallery/ml_gallery.cr"

gallery = MLGallery.new
gallery.start
gallery.close
19 changes: 12 additions & 7 deletions examples/ml_gallery/button_tab.cr
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ class ButtonTab < Hedron::Widget
HDML
end

def self.widget_name; return "ButtonTab"; end
def self.init_markup; return self.new; end
def self.widget_name
return "ButtonTab"
end

def self.init_markup
return self.new
end

def new_button
button = Hedron::HDML.render <<-HDML
Expand All @@ -35,20 +40,20 @@ class ButtonTab < Hedron::Widget
@window.not_nil!.message(title: "You clicked a button!", description: "You clicked #{this.text}.")

@buttons.delete_at(index)
@display.not_nil!["box"].widget.as(Hedron::VerticalBox).delete(index + 1)
@display.not_nil!["box"].as(Hedron::VerticalBox).delete(index + 1)
this.destroy
end

@buttons.push(button)

@counter += 1

@display.not_nil!["box"].widget.as(Hedron::VerticalBox).add(button)
@display.not_nil!["label"].widget.as(Hedron::Label).text = "You have made #{@counter} new buttons."
@display.not_nil!["box"].as(Hedron::VerticalBox).add(button)
@display.not_nil!["label"].as(Hedron::Label).text = "You have made #{@counter} new buttons."
end

def index=(i : String)
@index = i
@display.not_nil!["box"].widget.index = i
@display.not_nil!["box"].index = i
end
end
end
15 changes: 5 additions & 10 deletions examples/ml_gallery/ml_gallery.cr
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,22 @@ class MLGallery < Hedron::Application
return true
end

def initialize
super
def draw
self.on_stop = ->should_quit

Hedron::Classes.add_class(ButtonTab)
main = Hedron::HDML.render_file("./examples/ml_gallery/main.hdml")

@window = main["window"].widget.as(Hedron::Window)
@window = main["window"].as(Hedron::Window)
@window.not_nil!.on_close = ->on_closing(Hedron::Window)

main["btab"].widget.as(ButtonTab).window = @window.not_nil!
main["btab"].as(ButtonTab).window = @window.not_nil!

main["button"].widget.as(Hedron::Button).on_click do |button|
stats = main["btab"].widget.as(ButtonTab)
main["button"].as(Hedron::Button).on_click do |button|
stats = main["btab"].as(ButtonTab)
stats.new_button
end

@window.not_nil!.show
end
end

gallery = MLGallery.new
gallery.start
gallery.close
2 changes: 1 addition & 1 deletion shard.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors:
- Hanyuan Li <completelyderp@gmail.com>

description: |
An easy-to-use, OOP Crystal UI library.
A simple OOP UI library, with custom markup integration.
crystal: 0.24.1

Expand Down
4 changes: 3 additions & 1 deletion src/hedron/any.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
module Hedron
# `Hedron::Any` - the type of every parameter after being run
# through a HDML file.
alias Any = String | Int32 | Float64 | Bool
end
end
42 changes: 39 additions & 3 deletions src/hedron/application.cr
Original file line number Diff line number Diff line change
@@ -1,26 +1,58 @@
require "./bindings.cr"

module Hedron
class Application
# A basic class that allows people to create their own Hedron apps.
# To create a Hedron app, extend `Hedron::Application` and place all the
# UI instructions in the `initialize` method, and implement the `draw`
# method.
abstract class Application
@@box : Void*?

# Override this method to draw UI to the application itself.
#
# ```crystal
# class Foo < Hedron::Application
# def draw
# # Draw all of your UI here
# end
# end
# ```
abstract def draw

def initialize
options = UI::InitOptions.new
err = UI.init(pointerof(options))

unless ui_nil?(err)
raise UIError.new("Error initializing UI: #{err}")
end

draw
end

# Start up the application.
def start
UI.main
end

# Execute a set of instructions when the application
# is stopped.
#
# ```crystal
# on_stop do
# # Execute all instructions when stopped
# end
# ```
def on_stop(&block)
on_stop = block
self.on_stop = block
end

# Same as `on_stop(&block)`, except a function
# can be passed through this.
#
# ```crystal
# on_stop = foo # Here, foo is a Proc
# ```
def on_stop=(proc : Proc(Bool))
boxed_data = ::Box.box(proc)
@@box = boxed_data
Expand All @@ -33,22 +65,26 @@ module Hedron
UI.on_should_quit(new_proc, boxed_data)
end

# Stop the application
def stop
UI.quit
end

# Close the application
def close
UI.uninit
end

# Create a prompt that opens a file. Returns the file name.
def open_file(window : Window) : String?
chars = UI.open_file(window.to_unsafe)
return ui_nil?(chars) ? nil : String.new(chars)
end

# Create a prompt that saves a file. Returns the file name.
def save_file(window : Window) : String?
chars = UI.save_file(window.to_unsafe)
return ui_nil?(chars) ? nil : String.new(chars)
end
end
end
end
4 changes: 2 additions & 2 deletions src/hedron/bindings.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
@[Link(ldflags: "-lui")]
lib UI
UIDRAWDEFAULTMITERLIMIT = 10.0
UIPI = 3.14159265358979323846264338327950288419716939937510582097494459
UIDRAWDEFAULTMITERLIMIT = 10.0
UIPI = 3.14159265358979323846264338327950288419716939937510582097494459
AlignCenter = 2
AlignEnd = 3
AlignFill = 0
Expand Down
17 changes: 14 additions & 3 deletions src/hedron/classes.cr
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,29 @@ require "./ui/*"
require "./widget/widget.cr"

module Hedron
# Contains a list of classes that are used by the internal
# parser for HDML.
class Classes
CONTROL_CLASSES = [
Button, Checkbox, ColorButton, Combobox, DatePicker, DateTimePicker,
EditableCombobox, Entry, FontButton, Group, HorizontalBox, HorizontalSeparator,
Label, MultilineEntry, NonWrappingMultilineEntry, PasswordEntry, ProgressBar,
RadioButtons, SearchEntry, Separator, Slider, Spinbox, Tab, VerticalBox,
VerticalSeparator, Window
VerticalSeparator, Window,
] of Widget.class

class_property classes : Hash(String, Widget.class) =
CONTROL_CLASSES.map { |wclass| {wclass.widget_name, wclass} }.to_h
# Contains a list of all classes used by the parser,
# custom classes that extend Widget can be added to this
# variable.
class_getter classes : Hash(String, Widget.class) = CONTROL_CLASSES.map { |wclass| {wclass.widget_name, wclass} }.to_h

# Used when a custom class is to be added to `Hedron::Parser`.
# The class added must extend `Hedron::Widget`.
#
# ```crystal
# Hedron::Classes.add_class(Foo) # Foo < Hedron::Widget
# # Now you can use Foo in your HDML files and eval statements!
# ```
def self.add_class(wclass : Widget.class)
@@classes[wclass.widget_name] = wclass
end
Expand Down
2 changes: 1 addition & 1 deletion src/hedron/control.cr
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
require "./bindings.cr"

module Hedron
module Control
private module Control
setter stretchy = false

private def to_int(bool : Bool) : Int32
Expand Down
2 changes: 1 addition & 1 deletion src/hedron/exception/parser_error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@ module Hedron
super message
end
end
end
end
2 changes: 1 addition & 1 deletion src/hedron/exception/ui_error.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ module Hedron
super message
end
end
end
end
2 changes: 1 addition & 1 deletion src/hedron/hdml/hdml.cr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ module Hedron::HDML
def self.render(contents : String) : Render
return Parser.parse_tree(Lexer.lex_contents(contents))
end

def self.create_widget(class_name : String, id : String?, index : String?, values : MLArgs?, children : Array(Render)?) : Render
return Parser.parse_from_render(class_name, id, index, values, children)
end
Expand Down
16 changes: 8 additions & 8 deletions src/hedron/hdml/lexer.cr
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ module Hedron::HDML
id = /^#[a-z0-9\-]+\s*/m.match(control)

tree = if id.nil?
Tree.new(control_type)
else
temp = id.not_nil![0]
control = control[temp.size..-1]
Tree.new(control_type, temp.strip[1..-1])
end
Tree.new(control_type)
else
temp = id.not_nil![0]
control = control[temp.size..-1]
Tree.new(control_type, temp.strip[1..-1])
end

index = /^\^".+?"\s*/m.match(control)

Expand Down Expand Up @@ -54,7 +54,7 @@ module Hedron::HDML
content = control[1...end_index - 1]
new_index = end_index + /^\s*/i.match(control[end_index + 1..-1]).not_nil![0].size + 1
control = control[new_index..-1]

parsed_trees.push({tree, content})
end

Expand All @@ -72,7 +72,7 @@ module Hedron::HDML
content.clear if content == [""]
content = content[0..-2] if content.size > 0 && content[-1] == ""
index = nil

(0...content.size).each do |n|
if !content[n].includes?("{")
key, _, value = content[n].partition(':').map(&.strip)
Expand Down
Loading

0 comments on commit 9946822

Please sign in to comment.