-
-
Save rlipscombe/5f400451706efde62acbbd80700a6b7c to your computer and use it in GitHub Desktop.
#!/usr/bin/env elixir | |
defmodule Canvas do | |
@behaviour :wx_object | |
@title "Canvas Example" | |
@size {600, 600} | |
def start_link() do | |
:wx_object.start_link(__MODULE__, [], []) | |
end | |
def init(args \\ []) do | |
wx = :wx.new | |
frame = :wxFrame.new(wx, -1, @title, size: @size) | |
:wxFrame.connect(frame, :size) | |
:wxFrame.connect(frame, :close_window) | |
panel = :wxPanel.new(frame, []) | |
:wxPanel.connect(panel, :paint, [:callback]) | |
:wxFrame.show(frame) | |
state = %{panel: panel} | |
{frame, state} | |
end | |
def handle_event({:wx, _, _, _, {:wxSize, :size, size, _}}, state = %{panel: panel}) do | |
:wxPanel.setSize(panel, size) | |
{:noreply, state} | |
end | |
def handle_event({:wx, _, _, _, {:wxClose, :close_window}}, state) do | |
{:stop, :normal, state} | |
end | |
def handle_sync_event({:wx, _, _, _, {:wxPaint, :paint}}, _, state = %{panel: panel}) do | |
brush = :wxBrush.new | |
:wxBrush.setColour(brush, {255, 255, 255, 255}) | |
dc = :wxPaintDC.new(panel) | |
:wxDC.setBackground(dc, brush) | |
:wxDC.clear(dc) | |
:wxPaintDC.destroy(dc) | |
:ok | |
end | |
end | |
defmodule Script do | |
def main(args) do | |
{:wx_ref, _, _, pid} = Canvas.start_link | |
ref = Process.monitor(pid) | |
receive do | |
{:DOWN, ^ref, _, _, _} -> | |
:ok | |
end | |
end | |
end | |
Script.main(System.argv) |
Thank you for great sample code.
It helps a lot and gave me starting point to make elixir desktop application.
Pure erlang based, not external dependancy...GREATE
This sample was the proof of concept I needed to start doing GUI stuff with Elixir/Erlang.
Now I can take a load of fun, thanks to you.
[error] WX ERROR: Could not load library: :load_failed
Failed to load NIF library /usr/lib/erlang/lib/wx-2.1.2/priv/wxe_driver: 'libwx_gtk3u_webview-3.0.so.0: cannot open shared object file: No such file or directory'
** (EXIT from #PID<0.93.0>) an exception was raised:
** (ErlangError) Erlang error: {:error, {:load_failed, 'Failed to load NIF library /usr/lib/erlang/lib/wx-2.1.2/priv/wxe_driver: 'libwx_gtk3u_webview-3.0.so.0: cannot open shared object file: No such file or directory''}}
wxe_server.erl:65: :wxe_server.start/1
wx.erl:115: :wx.new/1
exwx.exs:14: Canvas.init/1
wx_object.erl:404: :wx_object.init_it/6
(stdlib 3.17.1) proc_lib.erl:226: :proc_lib.init_p_do_apply/3
Note that Ubuntu users getting the above error will need to install libwxgtk-webview3.0-gtk3-dev
I thought it might be interesting to write a turtle graphics "thing" (i.e. something like LOGO) in Elixir. A necessary step to that is rendering something to a canvas on the screen.
This is (I think) a minimal way to do that with Elixir and wxWidgets.
It's my first Elixir example of more than 5 lines, and the first time I've tried any wxWidgets (from Erlang, Elixir or otherwise). Constructive criticism welcome.