Skip to content

Commit

Permalink
Merge pull request #304 from BinaryNoggin/check-for-input-callback
Browse files Browse the repository at this point in the history
Warns missing `handle_input/3` on `request_input/2`
  • Loading branch information
crertel authored May 5, 2024
2 parents aa51267 + 46d9bbc commit 69fb409
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 1 deletion.
5 changes: 4 additions & 1 deletion lib/scenic/scene.ex
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,10 @@ defmodule Scenic.Scene do
def request_input(scene, input_class)
def request_input(scene, input) when is_atom(input), do: request_input(scene, [input])

def request_input(%Scene{viewport: vp, pid: pid}, inputs) when is_list(inputs) do
def request_input(%Scene{viewport: vp, pid: pid, module: module}, inputs) when is_list(inputs) do
unless Kernel.function_exported?(module, :handle_input, 3) do
Logger.warn("Requesting input for #{inspect inputs} - #{module}.handle_input/3 not implemented")

Check warning on line 727 in lib/scenic/scene.ex

View workflow job for this annotation

GitHub Actions / build (1.15.5, 26.0)

Logger.warn/1 is deprecated. Use Logger.warning/2 instead
end
ViewPort.Input.request(vp, inputs, pid: pid)
end

Expand Down
33 changes: 33 additions & 0 deletions test/scenic/scene_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ defmodule Scenic.SceneTest do
alias Scenic.Script
import Scenic.Components

import ExUnit.CaptureLog

@root_id ViewPort.root_id()

# import IEx
Expand Down Expand Up @@ -68,6 +70,23 @@ defmodule Scenic.SceneTest do
scene = assign(scene, pid: pid)
{:ok, scene}
end

@impl Scenic.Scene
def handle_input(input, id, %{assigns: %{pid: pid}} = scene) do
send(pid, {:input_test, input, id})
{:noreply, scene}
end
end

defmodule TestSceneNoInputHandler do
use Scenic.Scene

@impl Scenic.Scene
def init(scene, pid, _opts) do
Process.send(pid, {:up, scene}, [])
scene = assign(scene, pid: pid)
{:ok, scene}
end
end

@codepoint {:codepoint, {"k", []}}
Expand Down Expand Up @@ -402,6 +421,20 @@ defmodule Scenic.SceneTest do
~> {:ok, sorted_list([:cursor_pos, :cursor_button, :codepoint])}
end

test "request_input warns when handle_input is not implements" do
Scenic.Test.ViewPort.start({TestSceneNoInputHandler, self()})

scene =
receive do
{:up, scene} -> scene
end

assert capture_log(fn ->
Scenic.Scene.request_input(scene, [:key])
end)
=~ "handle_input/3 not implemented"
end

test "unrequest_input works", %{scene: scene} do
Scenic.Scene.request_input(scene, :cursor_button)

Expand Down
4 changes: 4 additions & 0 deletions test/scenic/view_port/input_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ defmodule Scenic.ViewPort.InputTest do
# def handle_input( input, %{assigns: %{pid: pid}} = scene ) do
# Process.send( pid, {:test_input, input}, [] )
# end

def handle_input(_, _, scene) do
{:noreply, scene}
end
end

setup do
Expand Down

0 comments on commit 69fb409

Please sign in to comment.