scarpe-wasm 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +7 -0
  2. data/CODE_OF_CONDUCT.md +84 -0
  3. data/Gemfile +13 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +39 -0
  6. data/Rakefile +12 -0
  7. data/lib/scarpe/wasm/alert.rb +65 -0
  8. data/lib/scarpe/wasm/app.rb +107 -0
  9. data/lib/scarpe/wasm/arc.rb +54 -0
  10. data/lib/scarpe/wasm/background.rb +27 -0
  11. data/lib/scarpe/wasm/border.rb +24 -0
  12. data/lib/scarpe/wasm/button.rb +50 -0
  13. data/lib/scarpe/wasm/check.rb +29 -0
  14. data/lib/scarpe/wasm/control_interface.rb +147 -0
  15. data/lib/scarpe/wasm/control_interface_test.rb +234 -0
  16. data/lib/scarpe/wasm/dimensions.rb +22 -0
  17. data/lib/scarpe/wasm/document_root.rb +8 -0
  18. data/lib/scarpe/wasm/edit_box.rb +44 -0
  19. data/lib/scarpe/wasm/edit_line.rb +43 -0
  20. data/lib/scarpe/wasm/flow.rb +24 -0
  21. data/lib/scarpe/wasm/font.rb +36 -0
  22. data/lib/scarpe/wasm/html.rb +108 -0
  23. data/lib/scarpe/wasm/image.rb +41 -0
  24. data/lib/scarpe/wasm/line.rb +35 -0
  25. data/lib/scarpe/wasm/link.rb +29 -0
  26. data/lib/scarpe/wasm/list_box.rb +50 -0
  27. data/lib/scarpe/wasm/para.rb +90 -0
  28. data/lib/scarpe/wasm/radio.rb +34 -0
  29. data/lib/scarpe/wasm/shape.rb +68 -0
  30. data/lib/scarpe/wasm/slot.rb +81 -0
  31. data/lib/scarpe/wasm/spacing.rb +41 -0
  32. data/lib/scarpe/wasm/span.rb +66 -0
  33. data/lib/scarpe/wasm/stack.rb +24 -0
  34. data/lib/scarpe/wasm/star.rb +61 -0
  35. data/lib/scarpe/wasm/subscription_item.rb +50 -0
  36. data/lib/scarpe/wasm/text_widget.rb +30 -0
  37. data/lib/scarpe/wasm/version.rb +7 -0
  38. data/lib/scarpe/wasm/video.rb +42 -0
  39. data/lib/scarpe/wasm/wasm_calls.rb +118 -0
  40. data/lib/scarpe/wasm/wasm_local_display.rb +94 -0
  41. data/lib/scarpe/wasm/web_wrangler.rb +679 -0
  42. data/lib/scarpe/wasm/webview_relay_display.rb +220 -0
  43. data/lib/scarpe/wasm/widget.rb +228 -0
  44. data/lib/scarpe/wasm/wv_display_worker.rb +75 -0
  45. data/lib/scarpe/wasm.rb +46 -0
  46. data/scarpe-wasm.gemspec +39 -0
  47. data/sig/scarpe/wasm.rbs +6 -0
  48. metadata +92 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1b3d86fe11d734c58ce7ab9cf656add25f9ddc66d10ebd0aa985c687449c4171
4
+ data.tar.gz: 6eaec0ea721002b29c768c973295ab0b1006a7799ff5d6202e0b8d61fe3f10a3
5
+ SHA512:
6
+ metadata.gz: ed0f48cdc6cba78634abd8c465e795532ffa755bc27158ae2f4e057530ea5793c1c4fba8f343a50159f81483f8d7788556d44af64527326312c6c5526df4d091
7
+ data.tar.gz: 8630be46bc9266669899c2f9a32226ccd08ea815ad6630793549a7af10953182ec66770b85bd14c842058f7187832e06029a3f533bc3a375f4b768662a4e1c07
@@ -0,0 +1,84 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
6
+
7
+ We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
8
+
9
+ ## Our Standards
10
+
11
+ Examples of behavior that contributes to a positive environment for our community include:
12
+
13
+ * Demonstrating empathy and kindness toward other people
14
+ * Being respectful of differing opinions, viewpoints, and experiences
15
+ * Giving and gracefully accepting constructive feedback
16
+ * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
17
+ * Focusing on what is best not just for us as individuals, but for the overall community
18
+
19
+ Examples of unacceptable behavior include:
20
+
21
+ * The use of sexualized language or imagery, and sexual attention or
22
+ advances of any kind
23
+ * Trolling, insulting or derogatory comments, and personal or political attacks
24
+ * Public or private harassment
25
+ * Publishing others' private information, such as a physical or email
26
+ address, without their explicit permission
27
+ * Other conduct which could reasonably be considered inappropriate in a
28
+ professional setting
29
+
30
+ ## Enforcement Responsibilities
31
+
32
+ Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
33
+
34
+ Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
35
+
36
+ ## Scope
37
+
38
+ This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
39
+
40
+ ## Enforcement
41
+
42
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at the.codefolio.guy@gmail.com. All complaints will be reviewed and investigated promptly and fairly.
43
+
44
+ All community leaders are obligated to respect the privacy and security of the reporter of any incident.
45
+
46
+ ## Enforcement Guidelines
47
+
48
+ Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
49
+
50
+ ### 1. Correction
51
+
52
+ **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
53
+
54
+ **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
55
+
56
+ ### 2. Warning
57
+
58
+ **Community Impact**: A violation through a single incident or series of actions.
59
+
60
+ **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
61
+
62
+ ### 3. Temporary Ban
63
+
64
+ **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
65
+
66
+ **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
67
+
68
+ ### 4. Permanent Ban
69
+
70
+ **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
71
+
72
+ **Consequence**: A permanent ban from any sort of public interaction within the community.
73
+
74
+ ## Attribution
75
+
76
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
77
+ available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
78
+
79
+ Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
80
+
81
+ [homepage]: https://www.contributor-covenant.org
82
+
83
+ For answers to common questions about this code of conduct, see the FAQ at
84
+ https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
data/Gemfile ADDED
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ source "https://rubygems.org"
4
+
5
+ # Specify your gem's dependencies in scarpe-folio.gemspec
6
+ gemspec
7
+
8
+ gem "scarpe", path: "../scarpe"
9
+
10
+ group :development do
11
+ gem "rake", "~> 13.0"
12
+ gem "minitest", "~> 5.0"
13
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2023 Noah Gibbs
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Scarpe::Wasm
2
+
3
+ Scarpe is a re-implementation of the Shoes GUI library for Ruby with more modern dependencies and with support for current Ruby versions. Scarpe supports different swappable display libraries, allowing it to run with various devices.
4
+
5
+ Using Scarpe you can turn a few lines of code into a small GUI application.
6
+
7
+ The Wasm display library for Scarpe was initially written by Giovanni Borgh, and adopted by the Scarpe team generally.
8
+
9
+ ## Installation
10
+
11
+ Install the gem and add to the application's Gemfile by executing:
12
+
13
+ $ bundle add scarpe-wasm
14
+
15
+ If bundler is not being used to manage dependencies, install the gem by executing:
16
+
17
+ $ gem install scarpe-wasm
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Development
24
+
25
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
26
+
27
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).
28
+
29
+ ## Contributing
30
+
31
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/scarpe-wasm. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/scarpe-wasm/blob/main/CODE_OF_CONDUCT.md).
32
+
33
+ ## License
34
+
35
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Scarpe::Wasm project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/scarpe-wasm/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rake/testtask"
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << "test"
8
+ t.libs << "lib"
9
+ t.test_files = FileList["test/**/test_*.rb"]
10
+ end
11
+
12
+ task default: :test
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Scarpe
4
+ class WASMAlert < WASMWidget
5
+ def initialize(properties)
6
+ super
7
+
8
+ bind("click") do
9
+ send_self_event(event_name: "click")
10
+ end
11
+ end
12
+
13
+ def element
14
+ onclick = handler_js_code("click")
15
+
16
+ HTML.render do |h|
17
+ h.div(id: html_id, style: overlay_style) do
18
+ h.div(style: modal_style) do
19
+ h.div(style: text_style) { @text }
20
+ h.button(style: button_style, onclick: onclick) { "OK" }
21
+ end
22
+ end
23
+ end
24
+ end
25
+
26
+ private
27
+
28
+ def overlay_style
29
+ {
30
+ position: "fixed",
31
+ top: "0",
32
+ left: "0",
33
+ width: "100%",
34
+ height: "100%",
35
+ overflow: "auto",
36
+ "z-index": "1",
37
+ background: "rgba(0,0,0,0.4)",
38
+ display: "flex",
39
+ "align-items": "center",
40
+ "justify-content": "center",
41
+ }
42
+ end
43
+
44
+ def modal_style
45
+ {
46
+ "min-width": "200px",
47
+ "min-height": "50px",
48
+ padding: "10px",
49
+ display: "flex",
50
+ background: "#fefefe",
51
+ "flex-direction": "column",
52
+ "justify-content": "space-between",
53
+ "border-radius": "9px",
54
+ }
55
+ end
56
+
57
+ def text_style
58
+ {}
59
+ end
60
+
61
+ def button_style
62
+ {}
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,107 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Scarpe
4
+ # Scarpe::WASMApp must only be used from the main thread, due to GTK+ limitations.
5
+ class WASMApp < WASMWidget
6
+ attr_reader :control_interface
7
+
8
+ attr_writer :shoes_linkable_id
9
+
10
+ def initialize(properties)
11
+ super
12
+
13
+ # It's possible to provide a Ruby script by setting
14
+ # SCARPE_TEST_CONTROL to its file path. This can
15
+ # allow pre-setting test options or otherwise
16
+ # performing additional actions not written into
17
+ # the Shoes app itself.
18
+ #
19
+ # The control interface is what lets these files see
20
+ # events, specify overrides and so on.
21
+ @control_interface = ControlInterface.new
22
+ if ENV["SCARPE_TEST_CONTROL"]
23
+ require "scarpe/unit_test_helpers"
24
+ @control_interface.instance_eval File.read(ENV["SCARPE_TEST_CONTROL"])
25
+ end
26
+
27
+ # TODO: rename @view
28
+ @view = Scarpe::WebWrangler.new title: @title,
29
+ width: @width,
30
+ height: @height,
31
+ resizable: @resizable
32
+
33
+ @callbacks = {}
34
+
35
+ # The control interface has to exist to get callbacks like "override Scarpe app opts".
36
+ # But the Scarpe App needs those options to be created. So we can't pass these to
37
+ # ControlInterface.new.
38
+ @control_interface.set_system_components app: self, doc_root: nil, wrangler: @view
39
+
40
+ bind_shoes_event(event_name: "init") { init }
41
+ bind_shoes_event(event_name: "run") { run }
42
+ bind_shoes_event(event_name: "destroy") { destroy }
43
+ end
44
+
45
+ attr_writer :document_root
46
+
47
+ def init
48
+ scarpe_app = self
49
+
50
+ @view.init_code("scarpeInit") do
51
+ request_redraw!
52
+ end
53
+
54
+ @view.bind("scarpeHandler") do |*args|
55
+ handle_callback(*args)
56
+ end
57
+
58
+ @view.bind("scarpeExit") do
59
+ scarpe_app.destroy
60
+ end
61
+ end
62
+
63
+ def run
64
+ @control_interface.dispatch_event(:init)
65
+
66
+ # This takes control of the main thread and never returns. And it *must* be run from
67
+ # the main thread. And it stops any Ruby background threads.
68
+ # That's totally cool and normal, right?
69
+ @view.run
70
+ end
71
+
72
+ def destroy
73
+ if @document_root || @view
74
+ @control_interface.dispatch_event :shutdown
75
+ end
76
+ @document_root = nil
77
+ if @view
78
+ @view.destroy
79
+ @view = nil
80
+ end
81
+ end
82
+
83
+ # All JS callbacks to Scarpe widgets are dispatched
84
+ # via this handler
85
+ def handle_callback(name, *args)
86
+ @callbacks[name].call(*args)
87
+ end
88
+
89
+ # Bind a Scarpe callback name; see handle_callback above.
90
+ # See Scarpe::Widget for how the naming is set up
91
+ def bind(name, &block)
92
+ @callbacks[name] = block
93
+ end
94
+
95
+ # Request a full redraw if WASM is running. Otherwise
96
+ # this is a no-op.
97
+ #
98
+ # @return [void]
99
+ def request_redraw!
100
+ wrangler = WASMDisplayService.instance.wrangler
101
+ if wrangler.is_running
102
+ wrangler.replace(@document_root.to_html)
103
+ end
104
+ nil
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Scarpe
4
+ class WASMArc < Scarpe::WASMWidget
5
+ def initialize(properties)
6
+ super(properties)
7
+ end
8
+
9
+ def element(&block)
10
+ HTML.render do |h|
11
+ h.div(id: html_id, style: style) do
12
+ h.svg(width: @width, height: @height) do
13
+ h.path(d: arc_path)
14
+ end
15
+ block.call(h) if block_given?
16
+ end
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def style
23
+ {
24
+ left: "#{@left}px",
25
+ top: "#{@top}px",
26
+ width: "#{@width}px",
27
+ height: "#{@height}px",
28
+ }
29
+ end
30
+
31
+ def arc_path
32
+ center_x = @width / 2
33
+ center_y = @height / 2
34
+ radius_x = @width / 2
35
+ radius_y = @height / 2
36
+ start_angle_degrees = radians_to_degrees(@angle1) % 360
37
+ end_angle_degrees = radians_to_degrees(@angle2) % 360
38
+ large_arc_flag = (end_angle_degrees - start_angle_degrees) % 360 > 180 ? 1 : 0
39
+
40
+ "M#{center_x} #{center_y} L#{@width} #{center_y} " \
41
+ "A#{radius_x} #{radius_y} 0 #{large_arc_flag} 0 " \
42
+ "#{center_x + radius_x * Math.cos(degrees_to_radians(end_angle_degrees))} " \
43
+ "#{center_y + radius_y * Math.sin(degrees_to_radians(end_angle_degrees))} Z"
44
+ end
45
+
46
+ def degrees_to_radians(degrees)
47
+ degrees * Math::PI / 180
48
+ end
49
+
50
+ def radians_to_degrees(radians)
51
+ radians * (180.0 / Math::PI)
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "scarpe/base64"
4
+
5
+ class Scarpe
6
+ module WASMBackground
7
+ include Base64
8
+
9
+ def style
10
+ styles = (super if defined?(super)) || {}
11
+ return styles unless @background_color
12
+
13
+ color = case @background_color
14
+ when Array
15
+ "rgba(#{@background_color.join(", ")})"
16
+ when Range
17
+ "linear-gradient(45deg, #{@background_color.first}, #{@background_color.last})"
18
+ when ->(value) { File.exist?(value) }
19
+ "url(data:image/png;base64,#{encode_file_to_base64(@background_color)})"
20
+ else
21
+ @background_color
22
+ end
23
+
24
+ styles.merge(background: color)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,24 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Scarpe
4
+ module WASMBorder
5
+ def style
6
+ styles = (super if defined?(super)) || {}
7
+ return styles unless @border_color
8
+
9
+ border_color = case @border_color
10
+ when Range
11
+ { "border-image": "linear-gradient(45deg, #{@border_color.first}, #{@border_color.last})" }
12
+ when Array
13
+ { "border-color": "rgba(#{@border_color.join(", ")})" }
14
+ else
15
+ { "border-color": @border_color }
16
+ end
17
+ styles.merge(
18
+ "border-style": "solid",
19
+ "border-width": "#{@options[:strokewidth] || 1}px",
20
+ "border-radius": "#{@options[:curve] || 0}px",
21
+ ).merge(border_color)
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Scarpe
4
+ class WASMButton < WASMWidget
5
+ def initialize(properties)
6
+ super
7
+
8
+ # Bind to display-side handler for "click"
9
+ bind("click") do
10
+ # This will be sent to the bind_self_event in Button
11
+ send_self_event(event_name: "click")
12
+ end
13
+ end
14
+
15
+ def element
16
+ HTML.render do |h|
17
+ h.button(id: html_id, onclick: handler_js_code("click"), style: style) do
18
+ @text
19
+ end
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def style
26
+ styles = {}
27
+ styles["background-color"] = @color
28
+ styles["padding-top"] = @padding_top
29
+ styles["padding-bottom"] = @padding_bottom
30
+ styles[:color] = @text_color
31
+ styles[:width] = Dimensions.length(@width) if @width
32
+ styles[:height] = Dimensions.length(@height) if @height
33
+ styles["font-size"] = @font_size
34
+
35
+ styles[:top] = Dimensions.length(@top) if @top
36
+ styles[:left] = Dimensions.length(@left) if @left
37
+ styles[:position] = "absolute" if @top || @left
38
+ styles["font-size"] = Dimensions.length(font_size) if @size
39
+ styles["font-family"] = @font if @font
40
+ styles["color"] = rgb_to_hex(@stroke) if @stroke
41
+ styles
42
+ end
43
+
44
+ def font_size
45
+ font_size = @size.is_a?(Symbol) ? SIZES[@size] : @size
46
+
47
+ Dimensions.length(font_size)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,29 @@
1
+ # frozen_string_literal: true
2
+
3
+ class Scarpe
4
+ class WASMCheck < Scarpe::WASMWidget
5
+ attr_reader :text
6
+
7
+ def initialize(properties)
8
+ super
9
+
10
+ bind("click") do
11
+ send_self_event(event_name: "click", target: shoes_linkable_id)
12
+ end
13
+ end
14
+
15
+ def properties_changed(changes)
16
+ checked = changes.delete("checked")
17
+
18
+ html_element.toggle_input_button(checked)
19
+
20
+ super
21
+ end
22
+
23
+ def element
24
+ HTML.render do |h|
25
+ h.input(type: :checkbox, id: html_id, onclick: handler_js_code("click"), value: "hmm #{text}", checked: @checked)
26
+ end
27
+ end
28
+ end
29
+ end