Skip to content
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 lua/flutter-tools.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ local function setup_commands()
command("FlutterRestart", commands.restart)
command("FlutterQuit", commands.quit)
command("FlutterVisualDebug", commands.visual_debug)
command("FlutterChangeTargetPlatform", commands.change_target_platform)
command("FlutterToggleBrightness", commands.brightness)
-- Lists
command("FlutterDevices", devices.list_devices)
command("FlutterEmulators", devices.list_emulators)
Expand Down
19 changes: 16 additions & 3 deletions lua/flutter-tools/commands.lua
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ local current_device = nil
---@field is_running fun(runner: flutter.Runner):boolean
---@field run fun(runner: flutter.Runner, opts: RunOpts, paths:table, args:table, cwd:string, on_run_data:fun(is_err:boolean, data:string), on_run_exit:fun(data:string[], args: table, opts: RunOpts?, project_conf: flutter.ProjectConfig?,launch_config: dap.Configuration?), is_flutter_project: boolean, project_conf: flutter.ProjectConfig?, launch_config: dap.Configuration?)
---@field cleanup fun(funner: flutter.Runner)
---@field send fun(runner: flutter.Runner, cmd:string, quiet: boolean?)
---@field send fun(runner: flutter.Runner, cmd:string, quiet: boolean?, on_response: fun(response: any)|nil)
---@field attach fun(runner: flutter.Runner, paths:table, args:table, cwd:string, on_run_data:fun(is_err:boolean, data:string), on_run_exit:fun(data:string[], args: table, project_conf: flutter.ProjectConfig?,launch_config: dap.Configuration?))

---@type flutter.Runner?
Expand Down Expand Up @@ -325,9 +325,10 @@ end
---@param cmd string
---@param quiet boolean?
---@param on_send function|nil
local function send(cmd, quiet, on_send)
---@param on_response fun(response:any)|nil
local function send(cmd, quiet, on_send, on_response)
if M.is_running() and runner then
runner:send(cmd, quiet)
runner:send(cmd, quiet, on_response)
if on_send then on_send() end
elseif not quiet then
ui.notify("Sorry! Flutter is not running")
Expand All @@ -354,6 +355,18 @@ function M.quit(quiet)
end)
end

---@param quiet boolean?
function M.change_target_platform(quiet)
send("change_target_platform", quiet, nil, function(response)
if not quiet then
ui.notify("Changing platform to " .. response.value, nil, { timeout = 1500 })
end
end)
end

---@param quiet boolean?
function M.brightness(quiet) send("brightness", quiet) end

---@param quiet boolean?
function M.visual_debug(quiet) send("visual_debug", quiet) end

Expand Down
26 changes: 24 additions & 2 deletions lua/flutter-tools/runners/debugger_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,22 @@ local function register_default_configurations(paths, is_flutter_project, projec
end
end

local function get_current_value(cmd)
local service_activation_params = vm_service_extensions.get_request_params(cmd)
if
not service_activation_params
or not service_activation_params.params.isolateId
then return end

service_activation_params.params = {
isolateId = service_activation_params.params.isolateId
}
dap.session():request("callService", service_activation_params, function(err, result)
if err then return end
vm_service_extensions.set_service_extensions_state(result.method, result.value)
end)
end

local function register_dap_listeners(on_run_data, on_run_exit)
local started = false
local before_start_logs = {}
Expand Down Expand Up @@ -133,6 +149,11 @@ local function register_dap_listeners(on_run_data, on_run_exit)
dap.listeners.before["event_dart.serviceExtensionAdded"][plugin_identifier] = function(_, body)
if body and body.extensionRPC and body.isolateId then
vm_service_extensions.set_isolate_id(body.extensionRPC, body.isolateId)
if body.extensionRPC == "ext.flutter.brightnessOverride" then
get_current_value("brightness")
elseif body.extensionRPC == "ext.flutter.platformOverride" then
get_current_value("change_target_platform")
end
end
end

Expand Down Expand Up @@ -280,7 +301,7 @@ function DebuggerRunner:attach(paths, args, cwd, on_run_data, on_run_exit)
end
end

function DebuggerRunner:send(cmd, quiet)
function DebuggerRunner:send(cmd, quiet, on_response)
if cmd == "open_dev_tools" then
dev_tools.open_dev_tools()
return
Expand All @@ -292,10 +313,11 @@ function DebuggerRunner:send(cmd, quiet)
end
local service_activation_params = vm_service_extensions.get_request_params(cmd)
if service_activation_params then
dap.session():request("callService", service_activation_params, function(err, _)
dap.session():request("callService", service_activation_params, function(err, response)
if err and not quiet then
ui.notify("Error calling service " .. cmd .. ": " .. err, ui.ERROR)
end
if response and on_response then on_response(response) end
end)
return
end
Expand Down
2 changes: 2 additions & 0 deletions lua/flutter-tools/runners/job_runner.lua
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ local command_keys = {
paint_baselines = "p",
open_dev_tools = "v",
generate = "g",
change_target_platform = "o",
brightness = "b",
}

function JobRunner:is_running() return run_job ~= nil end
Expand Down
19 changes: 19 additions & 0 deletions lua/flutter-tools/runners/vm_service_extensions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ local service_activation_requests = {
slow_animations = "ext.flutter.timeDilation",
inspect_widget = "ext.flutter.inspector.show",
paint_baselines = "ext.flutter.debugPaintBaselinesEnabled",
change_target_platform = "ext.flutter.platformOverride",
brightness = "ext.flutter.brightnessOverride",
}

local toggle_extension_state_keys = {
Expand All @@ -29,10 +31,27 @@ local toggle_extension_state_keys = {
slow_animations = "timeDilation",
inspect_widget = "enabled",
paint_baselines = "enabled",
change_target_platform = "value",
brightness = "value",
}

local cicle_target_platform = {
linux = "fuchsia",
fuchsia = "android",
android = "iOS",
iOS = "windows",
windows = "macOS",
macOS = "linux",
}

local function toggle_value(request)
local value = service_extensions_state[request]
if request == service_activation_requests.change_target_platform then
return cicle_target_platform[value] or "linux"
end
if request == service_activation_requests.brightness then
return "Brightness." .. (value == "Brightness.light" and "dark" or "light")
end
if request == service_activation_requests.slow_animations then
if value == "5.0" then
return "1.0"
Expand Down