@@ -58,45 +58,75 @@ pub const rpc_methods = struct {
5858 }
5959};
6060
61+ const BrowserLaunchPreference = enum {
62+ auto ,
63+ app_window ,
64+ web_tab ,
65+ };
66+
67+ const RunModeSpec = struct {
68+ launch_policy : webui.LaunchPolicy ,
69+ browser_launch_preference : BrowserLaunchPreference ,
70+ };
71+
6172fn parseSurfaceToken (token : []const u8 ) ? webui.LaunchSurface {
6273 if (std .mem .eql (u8 , token , "webview" )) return .native_webview ;
6374 if (std .mem .eql (u8 , token , "browser" )) return .browser_window ;
64- if (std .mem .eql (u8 , token , "web-tab" )) return .web_url ;
65- if (std .mem .eql (u8 , token , "web" )) return .web_url ;
66- if (std .mem .eql (u8 , token , "web-url" )) return .web_url ;
75+ if (std .mem .eql (u8 , token , "web-tab" ) or std .mem .eql (u8 , token , "web-url" )) return .web_url ;
6776 return null ;
6877}
6978
70- fn launchPolicyFromRunModeValue (mode : []const u8 ) webui.LaunchPolicy {
71- if (std .mem .eql (u8 , mode , "webview" )) return .{
72- .first = .native_webview ,
73- .second = null ,
74- .third = null ,
75- .allow_dual_surface = false ,
76- .app_mode_required = true ,
77- };
78- if (std .mem .eql (u8 , mode , "browser" )) return .{
79- .first = .browser_window ,
80- .second = null ,
81- .third = null ,
82- .allow_dual_surface = false ,
83- .app_mode_required = true ,
79+ fn launchPolicyForSingleSurface (surface : webui.LaunchSurface ) webui.LaunchPolicy {
80+ return switch (surface ) {
81+ .native_webview = > .{
82+ .first = .native_webview ,
83+ .second = null ,
84+ .third = null ,
85+ .allow_dual_surface = false ,
86+ .app_mode_required = true ,
87+ },
88+ .browser_window = > .{
89+ .first = .browser_window ,
90+ .second = null ,
91+ .third = null ,
92+ .allow_dual_surface = false ,
93+ .app_mode_required = true ,
94+ },
95+ .web_url = > webui .LaunchPolicy .webUrlOnly (),
8496 };
85- if (std .mem .eql (u8 , mode , "web-tab" )) return webui .LaunchPolicy .webUrlOnly ();
86- if (std .mem .eql (u8 , mode , "web" )) return webui .LaunchPolicy .webUrlOnly ();
87- if (std .mem .eql (u8 , mode , "web-url" )) {
88- return webui .LaunchPolicy .webUrlOnly ();
97+ }
98+
99+ fn parseRunModeSpec (mode : []const u8 ) RunModeSpec {
100+ if (parseSurfaceToken (mode )) | single_surface | {
101+ return .{
102+ .launch_policy = launchPolicyForSingleSurface (single_surface ),
103+ .browser_launch_preference = if (std .mem .eql (u8 , mode , "browser" ))
104+ .app_window
105+ else if (std .mem .eql (u8 , mode , "web-tab" ))
106+ .web_tab
107+ else
108+ .auto ,
109+ };
89110 }
90111
91112 var surfaces : [3 ]? webui.LaunchSurface = .{ null , null , null };
92113 var count : usize = 0 ;
114+ var launch_pref : BrowserLaunchPreference = .auto ;
93115
94116 var it = std .mem .tokenizeAny (u8 , mode , ",> " );
95117 while (it .next ()) | raw_token | {
96118 const token = std .mem .trim (u8 , raw_token , " \t \r \n " );
97119 if (token .len == 0 ) continue ;
98120 const parsed = parseSurfaceToken (token ) orelse continue ;
99121
122+ if (launch_pref == .auto ) {
123+ if (std .mem .eql (u8 , token , "browser" )) {
124+ launch_pref = .app_window ;
125+ } else if (std .mem .eql (u8 , token , "web-tab" )) {
126+ launch_pref = .web_tab ;
127+ }
128+ }
129+
100130 var exists = false ;
101131 for (surfaces ) | candidate | {
102132 if (candidate != null and candidate .? == parsed ) {
@@ -110,7 +140,12 @@ fn launchPolicyFromRunModeValue(mode: []const u8) webui.LaunchPolicy {
110140 count += 1 ;
111141 }
112142
113- if (count == 0 ) return webui .LaunchPolicy .webviewFirst ();
143+ if (count == 0 ) {
144+ return .{
145+ .launch_policy = webui .LaunchPolicy .webviewFirst (),
146+ .browser_launch_preference = .auto ,
147+ };
148+ }
114149
115150 var policy = webui.LaunchPolicy {
116151 .first = surfaces [0 ].? ,
@@ -122,31 +157,12 @@ fn launchPolicyFromRunModeValue(mode: []const u8) webui.LaunchPolicy {
122157 const has_native = policy .first == .native_webview or
123158 (policy .second != null and policy .second .? == .native_webview ) or
124159 (policy .third != null and policy .third .? == .native_webview );
125- if (! has_native ) {
126- policy .app_mode_required = false ;
127- }
128- return policy ;
129- }
160+ if (! has_native ) policy .app_mode_required = false ;
130161
131- const BrowserLaunchPreference = enum {
132- auto ,
133- app_window ,
134- web_tab ,
135- };
136-
137- fn browserLaunchPreferenceFromRunMode (mode : []const u8 ) BrowserLaunchPreference {
138- if (std .mem .eql (u8 , mode , "browser" )) return .app_window ;
139- if (std .mem .eql (u8 , mode , "web-tab" )) return .web_tab ;
140- if (std .mem .eql (u8 , mode , "web" )) return .web_tab ;
141-
142- var it = std .mem .tokenizeAny (u8 , mode , ",> " );
143- while (it .next ()) | raw_token | {
144- const token = std .mem .trim (u8 , raw_token , " \t \r \n " );
145- if (token .len == 0 ) continue ;
146- if (std .mem .eql (u8 , token , "browser" )) return .app_window ;
147- if (std .mem .eql (u8 , token , "web-tab" ) or std .mem .eql (u8 , token , "web" )) return .web_tab ;
148- }
149- return .auto ;
162+ return .{
163+ .launch_policy = policy ,
164+ .browser_launch_preference = launch_pref ,
165+ };
150166}
151167
152168fn surfaceName (surface : webui.LaunchSurface ) []const u8 {
@@ -299,8 +315,9 @@ fn tagFor(comptime kind: ExampleKind) []const u8 {
299315
300316fn appOptionsFor (comptime kind : ExampleKind ) webui.AppOptions {
301317 const run_mode = webui .BuildFlags .run_mode ;
302- const launch_policy = launchPolicyFromRunModeValue (run_mode );
303- const launch_pref = browserLaunchPreferenceFromRunMode (run_mode );
318+ const run_mode_spec = parseRunModeSpec (run_mode );
319+ const launch_policy = run_mode_spec .launch_policy ;
320+ const launch_pref = run_mode_spec .browser_launch_preference ;
304321 const native_first = launch_policy .first == .native_webview ;
305322 var surface_mode : webui.BrowserSurfaceMode = if (native_first ) .native_webview_host else .tab ;
306323 var fallback_mode : webui.BrowserFallbackMode = if (native_first ) .strict else .allow_system ;
@@ -340,45 +357,43 @@ fn linuxWebViewTargetFromBuildFlag() webui.LinuxWebViewTarget {
340357}
341358
342359test "run-mode browser launch preference parser supports web-tab" {
343- try std .testing .expectEqual (BrowserLaunchPreference .app_window , browserLaunchPreferenceFromRunMode ("browser" ));
344- try std .testing .expectEqual (BrowserLaunchPreference .web_tab , browserLaunchPreferenceFromRunMode ("web-tab" ));
345- try std .testing .expectEqual (BrowserLaunchPreference .web_tab , browserLaunchPreferenceFromRunMode ("web" ));
346- try std .testing .expectEqual (BrowserLaunchPreference .web_tab , browserLaunchPreferenceFromRunMode ("webview,web-tab,web-url" ));
347- try std .testing .expectEqual (BrowserLaunchPreference .web_tab , browserLaunchPreferenceFromRunMode ("webview,web,web-url" ));
348- try std .testing .expectEqual (BrowserLaunchPreference .auto , browserLaunchPreferenceFromRunMode ("web-url" ));
360+ try std .testing .expectEqual (BrowserLaunchPreference .app_window , parseRunModeSpec ("browser" ).browser_launch_preference );
361+ try std .testing .expectEqual (BrowserLaunchPreference .web_tab , parseRunModeSpec ("web-tab" ).browser_launch_preference );
362+ try std .testing .expectEqual (BrowserLaunchPreference .web_tab , parseRunModeSpec ("webview,web-tab,web-url" ).browser_launch_preference );
363+ try std .testing .expectEqual (BrowserLaunchPreference .auto , parseRunModeSpec ("web-url" ).browser_launch_preference );
349364}
350365
351366test "run-mode browser maps to browser-first launch policy" {
352- const policy = launchPolicyFromRunModeValue ("browser" );
367+ const policy = parseRunModeSpec ("browser" ). launch_policy ;
353368 try std .testing .expectEqual (@as (webui .LaunchSurface , .browser_window ), policy .first );
354369 try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), policy .second );
355370 try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), policy .third );
356371 try std .testing .expect (policy .app_mode_required );
357372}
358373
359- test "run-mode web alias maps to web-url only launch policy" {
360- const policy = launchPolicyFromRunModeValue ("web" ) ;
374+ test "run-mode web-url maps to web-url only launch policy" {
375+ const policy = parseRunModeSpec ("web-url" ). launch_policy ;
361376 try std .testing .expectEqual (@as (webui .LaunchSurface , .web_url ), policy .first );
362377 try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), policy .second );
363378 try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), policy .third );
364379 try std .testing .expect (! policy .app_mode_required );
365380}
366381
367382test "single-token run-mode presets are strict with no fallback surfaces" {
368- const webview_policy = launchPolicyFromRunModeValue ("webview" );
383+ const webview_policy = parseRunModeSpec ("webview" ). launch_policy ;
369384 try std .testing .expectEqual (@as (webui .LaunchSurface , .native_webview ), webview_policy .first );
370385 try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), webview_policy .second );
371386 try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), webview_policy .third );
372387
373- const browser_policy = launchPolicyFromRunModeValue ("browser" );
388+ const browser_policy = parseRunModeSpec ("browser" ). launch_policy ;
374389 try std .testing .expectEqual (@as (webui .LaunchSurface , .browser_window ), browser_policy .first );
375390 try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), browser_policy .second );
376391 try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), browser_policy .third );
377392
378- const web_policy = launchPolicyFromRunModeValue ("web" ) ;
379- try std .testing .expectEqual (@as (webui .LaunchSurface , .web_url ), web_policy .first );
380- try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), web_policy .second );
381- try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), web_policy .third );
393+ const web_url_policy = parseRunModeSpec ("web-url" ). launch_policy ;
394+ try std .testing .expectEqual (@as (webui .LaunchSurface , .web_url ), web_url_policy .first );
395+ try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), web_url_policy .second );
396+ try std .testing .expectEqual (@as (? webui .LaunchSurface , null ), web_url_policy .third );
382397}
383398
384399fn styleFor (comptime kind : ExampleKind ) webui.WindowStyle {
0 commit comments