-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #292 from scarpe-team/mouse_motion_spike
Spike for events, including mouse motion
- Loading branch information
Showing
19 changed files
with
259 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
Shoes.app width: 600, height: 600 do | ||
stack do | ||
para "Events and Menus" | ||
flow do | ||
@btn1 = button "button 1", width: 75 do | ||
@eb.append "button 1 clicked\n" | ||
end | ||
click do | ||
@eb.append "flow click\n" | ||
end | ||
hover do | ||
@eb.append "flow hover\n" | ||
end | ||
end | ||
@eb = edit_box width: 500, height: 350 | ||
end | ||
motion do |x, y, mods| | ||
@eb.append "motion #{x},#{y} #{mods} " | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ | |
class Scarpe::Slot < Scarpe::Widget | ||
include Scarpe::Background | ||
include Scarpe::Border | ||
include Scarpe::Spacing | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
# frozen_string_literal: true | ||
|
||
# Certain Shoes calls like motion and keydown are basically an | ||
# event subscription, with no other visible presence. However, | ||
# they have a place in the widget tree and can be deleted. | ||
# | ||
# Depending on the display library they may not have any | ||
# direct visual (or similar) presence there either. | ||
# | ||
# Inheriting from Widget gives these a parent slot and a | ||
# linkable_id automatically. | ||
class SubscriptionItem < Scarpe::Widget | ||
display_property :shoes_api_name | ||
|
||
def initialize(shoes_api_name:, &block) | ||
super | ||
|
||
@callback = block | ||
|
||
case shoes_api_name | ||
when "hover" | ||
# Hover passes the Shoes widget as the block param | ||
@unsub_id = bind_self_event("hover") do | ||
@callback&.call(self) | ||
end | ||
when "motion" | ||
# Shoes sends back x, y, mods as the args. | ||
# Shoes3 uses the strings "control" "shift" and | ||
# "control_shift" as the mods arg. | ||
@unsub_id = bind_self_event("motion") do |x, y, ctrl_key, shift_key, **_kwargs| | ||
mods = [ctrl_key ? "control" : nil, shift_key ? "shift" : nil].compact.join("_") | ||
@callback&.call(x, y, mods) | ||
end | ||
when "click" | ||
# Click has block params button, left, top | ||
# button is the button number, left and top are coords | ||
@unsub_id = bind_self_event("click") do |button, x, y, **_kwargs| | ||
@callback&.call(button, x, y) | ||
end | ||
else | ||
raise "Unknown Shoes API call #{shoes_api_name.inspect} passed to SubscriptionItem!" | ||
end | ||
|
||
@unsub_id = bind_self_event(shoes_api_name) do |*args| | ||
@callback&.call(*args) | ||
end | ||
|
||
# This won't create a visible display widget, but will turn into | ||
# an invisible widget and a stream of events. | ||
create_display_widget | ||
end | ||
|
||
def destroy | ||
# TODO: we need a better way to do this automatically. See https://github.com/scarpe-team/scarpe/issues/291 | ||
unsub_shoes_event(@unsub_id) if @unsub_id | ||
@unsub_id = nil | ||
|
||
super | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# frozen_string_literal: true | ||
|
||
class Scarpe | ||
class WebviewSlot < Scarpe::WebviewWidget | ||
include Scarpe::WebviewBackground | ||
include Scarpe::WebviewBorder | ||
include Scarpe::WebviewSpacing | ||
|
||
def initialize(properties) | ||
@event_callbacks = {} | ||
|
||
super | ||
end | ||
|
||
def element(&block) | ||
HTML.render do |h| | ||
h.div(attributes.merge(id: html_id, style: style), &block) | ||
end | ||
end | ||
|
||
def set_event_callback(obj, event_name, js_code) | ||
event_name = event_name.to_s | ||
@event_callbacks[event_name] ||= {} | ||
if @event_callbacks[event_name][obj] | ||
raise "Can't have two callbacks on the same event, from the same object, on the same parent!" | ||
end | ||
|
||
@event_callbacks[event_name][obj] = js_code | ||
|
||
update_dom_event(event_name) | ||
end | ||
|
||
def remove_event_callback(obj, event_name) | ||
event_name = event_name.to_s | ||
@event_callbacks[event_name] ||= {} | ||
@event_callbacks[event_name].delete(obj) | ||
|
||
update_dom_event(event_name) | ||
end | ||
|
||
def remove_event_callbacks(obj) | ||
changed = [] | ||
|
||
@event_callbacks.each do |event_name, items| | ||
changed << event_name if items.delete(obj) | ||
end | ||
|
||
changed.each { |event_name| update_dom_event(event_name) } | ||
end | ||
|
||
protected | ||
|
||
def update_dom_event(event_name) | ||
html_element.set_attribute(event_name, @event_callbacks[event_name].values.join(";")) | ||
end | ||
|
||
def attributes | ||
attr = {} | ||
|
||
@event_callbacks.each do |event_name, handlers| | ||
attr[event_name] = handlers.values.join(";") | ||
end | ||
|
||
attr | ||
end | ||
|
||
def style | ||
styles = super | ||
|
||
styles["margin-top"] = @margin_top if @margin_top | ||
styles["margin-bottom"] = @margin_bottom if @margin_bottom | ||
styles["margin-left"] = @margin_left if @margin_left | ||
styles["margin-right"] = @margin_right if @margin_right | ||
|
||
styles[:width] = Dimensions.length(@width) if @width | ||
styles[:height] = Dimensions.length(@height) if @height | ||
|
||
styles | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.