Skip to content

Basic app #1

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

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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 .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,7 @@ build-iPhoneSimulator/

*.dll
*.so
*.exp
*.lib

.vscode
88 changes: 88 additions & 0 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added .ruby-version
Binary file not shown.
15 changes: 12 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,30 @@

#### Windows

I recommend to install Ruby via [Scoop](https://scoop.sh/), then
I recommend to install Ruby via [winget](https://learn.microsoft.com/en-us/windows/package-manager/winget/):

`winget install RubyInstallerTeam.RubyWithDevKit.3.2`

The reason why you need the dev kit is that the event machine gem needs to be compiled on the target machine.

Then install the dependencies (you likely need to close all terminals first):

- `gem install ffi`
- `gem install eventmachine`
- `gem install rx`

`RUBY_DLL_PATH` must be set:
`RUBY_DLL_PATH` must be set, e.g.:

`$env:RUBY_DLL_PATH="C:\dev\xframes-ruby"`

For convenience, you may launch `main.bat` or `main.ps1` depending on whether you are using a regular command line or PowerShell.

#### Linux

- `sudo apt install ruby-full`
- `sudo gem install ffi`
- `sudo gem install eventmachine`
- `sudo gem install rx`

### Run the application

Expand All @@ -34,4 +44,3 @@ Windows 11
Raspberry Pi 5

![image](https://github.com/user-attachments/assets/190f8603-a6db-45c6-a5f0-cfd4dc1b87e2)

4 changes: 4 additions & 0 deletions main.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@echo off
set RUBY_DLL_PATH=%CD%
echo RUBY_DLL_PATH set to %RUBY_DLL_PATH%
ruby main.rb
3 changes: 3 additions & 0 deletions main.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
$env:RUBY_DLL_PATH = Get-Location
Write-Output "RUBY_DLL_PATH set to $env:RUBY_DLL_PATH"
ruby main.rb
151 changes: 13 additions & 138 deletions main.rb
Original file line number Diff line number Diff line change
@@ -1,63 +1,13 @@
require 'ffi'
require 'json'
require 'thread'
require 'concurrent-ruby'
require 'eventmachine'

ImGuiCol = {
Text: 0,
TextDisabled: 1,
WindowBg: 2,
ChildBg: 3,
PopupBg: 4,
Border: 5,
BorderShadow: 6,
FrameBg: 7,
FrameBgHovered: 8,
FrameBgActive: 9,
TitleBg: 10,
TitleBgActive: 11,
TitleBgCollapsed: 12,
MenuBarBg: 13,
ScrollbarBg: 14,
ScrollbarGrab: 15,
ScrollbarGrabHovered: 16,
ScrollbarGrabActive: 17,
CheckMark: 18,
SliderGrab: 19,
SliderGrabActive: 20,
Button: 21,
ButtonHovered: 22,
ButtonActive: 23,
Header: 24,
HeaderHovered: 25,
HeaderActive: 26,
Separator: 27,
SeparatorHovered: 28,
SeparatorActive: 29,
ResizeGrip: 30,
ResizeGripHovered: 31,
ResizeGripActive: 32,
Tab: 33,
TabHovered: 34,
TabActive: 35,
TabUnfocused: 36,
TabUnfocusedActive: 37,
PlotLines: 38,
PlotLinesHovered: 39,
PlotHistogram: 40,
PlotHistogramHovered: 41,
TableHeaderBg: 42,
TableBorderStrong: 43,
TableBorderLight: 44,
TableRowBg: 45,
TableRowBgAlt: 46,
TextSelectedBg: 47,
DragDropTarget: 48,
NavHighlight: 49,
NavWindowingHighlight: 50,
NavWindowingDimBg: 51,
ModalWindowDimBg: 52,
COUNT: 53
}
require_relative 'theme'
require_relative 'sampleapp'
require_relative 'services'
require_relative 'treetraversal'
require_relative 'xframes'

# Colors for theme generation
theme2Colors = {
Expand Down Expand Up @@ -144,90 +94,13 @@
font_defs_json = JSON.pretty_generate(defs: font_size_pairs)
theme_json = JSON.pretty_generate(theme2)

class Node
attr_accessor :id, :root

def initialize(id, root)
@type = 'node'
@id = id
@root = root
end

def to_json(*options)
{
type: @type,
id: @id,
root: @root
}.to_json(*options)
end
end

class UnformattedText
attr_accessor :id, :text

def initialize(id, text)
@type = 'unformatted-text'
@id = id
@text = text
end

def to_json(*options)
{
type: @type,
id: @id,
text: @text
}.to_json(*options)
end
end


module XFrames
extend FFI::Library
if RUBY_PLATFORM =~ /win32|mingw|cygwin/
ffi_lib './xframesshared.dll'
else
ffi_lib './libxframesshared.so'
end

# Define callback types
callback :OnInitCb, [:pointer], :void
callback :OnTextChangedCb, [:int, :string], :void
callback :OnComboChangedCb, [:int, :int], :void
callback :OnNumericValueChangedCb, [:int, :float], :void
callback :OnBooleanValueChangedCb, [:int, :int], :void
callback :OnMultipleNumericValuesChangedCb, [:int, :pointer, :int], :void
callback :OnClickCb, [:int], :void

attach_function :init, [
:string, # assetsBasePath
:string, # rawFontDefinitions
:string, # rawStyleOverrideDefinitions
:OnInitCb,
:OnTextChangedCb,
:OnComboChangedCb,
:OnNumericValueChangedCb,
:OnBooleanValueChangedCb,
:OnMultipleNumericValuesChangedCb,
:OnClickCb
], :void

attach_function :setElement, [:string], :void

attach_function :setChildren, [:int, :string], :void
end
service = WidgetRegistrationService.new
shadow_node_traversal_helper = ShadowNodeTraversalHelper.new(service)

on_init = FFI::Function.new(:void, []) do
puts "OnInit called!"

node = Node.new(0, true)
unformatted_text = UnformattedText.new(1, "Hello, world")
root = Root.new()

children_ids = [1]

XFrames.setElement(node.to_json)
XFrames.setElement(unformatted_text.to_json)

XFrames.setChildren(0, children_ids.to_json)
shadow_node_traversal_helper.traverse_tree(root)
end

on_text_changed = FFI::Function.new(:void, [:int, :string]) do |id, text|
Expand All @@ -252,6 +125,8 @@ module XFrames
end

on_click = FFI::Function.new(:void, [:int]) do |id|
service.dispatch_on_click_event(id)

puts "Button clicked: ID=#{id}"
end

Expand Down
Loading