advanced desktop mode

This commit is contained in:
Fabian Freund
2026-06-06 15:39:09 +02:00
parent 4a12acab99
commit 370c422e12
23 changed files with 732 additions and 78 deletions
@@ -17,9 +17,14 @@
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:async';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:weblibre/features/geckoview/domain/providers/selected_tab.dart';
import 'package:weblibre/features/geckoview/domain/providers/tab_session.dart';
import 'package:weblibre/features/geckoview/domain/providers/tab_state.dart';
import 'package:weblibre/features/user/domain/repositories/general_settings.dart';
import 'package:weblibre/utils/host_rules.dart';
part 'desktop_mode.g.dart';
@@ -34,6 +39,16 @@ class DesktopMode extends _$DesktopMode {
state = !state;
}
/// Resolves the desktop-mode state a tab on [host] should have, honouring the
/// per-site rule list first and falling back to the browser-wide default.
bool _resolveForHost(Uri? url) {
final settings = ref.read(generalSettingsWithDefaultsProvider);
if (url != null && hostMatchesRule(url, settings.desktopModeSites)) {
return true;
}
return settings.globalDesktopMode;
}
@override
bool build(String tabId) {
listenSelf((previous, next) async {
@@ -44,11 +59,62 @@ class DesktopMode extends _$DesktopMode {
}
});
// Seed the initial value from the browser-wide default so a newly opened
// tab's menu checkbox matches the desktop mode it was actually created with
// natively (GeckoTabsApi seeds new tabs from BrowserState.desktopMode).
// Read (not watch) so toggling the global default never clobbers an
// existing tab's per-tab override.
return ref.read(generalSettingsWithDefaultsProvider).globalDesktopMode;
// Re-apply the per-site rule whenever the tab's host changes. A manual
// toggle from the menu therefore lasts only for the current host-visit:
// landing on a ruled host forces desktop on again, and leaving it reverts
// to the browser-wide default. Watching only the host keeps in-page
// navigations (path/query changes) from clobbering a manual override.
ref.listen(
tabStateProvider(tabId),
(previous, next) {
if (previous?.url.host != next?.url.host) {
state = _resolveForHost(next?.url);
}
},
);
// Seed the initial value from the per-site rule (falling back to the
// browser-wide default) so a newly opened tab's menu checkbox matches the
// desktop mode it was actually created with natively (GeckoTabsApi seeds
// new tabs from BrowserState.desktopMode). Read (not watch) so toggling the
// global default never clobbers an existing tab's per-tab override.
final resolved = _resolveForHost(ref.read(tabStateProvider(tabId))?.url);
// The engine seeds a tab's desktop mode from the browser-wide default at
// creation, and a global-default change re-applies to every tab, so at the
// moment this notifier (re)builds the engine holds [globalDesktopMode]. When
// a per-site rule resolves to a different value, push it now: the listenSelf
// guard above skips the initial seed, so without this the rule would never
// reach Gecko unless the host later changed while this notifier was alive
// (which only happens when a menu/sheet kept it mounted across a navigation).
final globalDesktopMode = ref
.read(generalSettingsWithDefaultsProvider)
.globalDesktopMode;
if (resolved != globalDesktopMode) {
unawaited(
Future.microtask(() async {
if (!ref.mounted) return;
await ref
.read(tabSessionProvider(tabId: tabId).notifier)
.requestDesktopSite(resolved);
}),
);
}
return resolved;
}
}
/// Always-mounted helper that instantiates the selected tab's [DesktopMode]
/// notifier so the per-site desktop-mode rule is applied on navigation even when
/// no tab menu or site sheet is open. Mounted by the browser view. [DesktopMode]
/// is keepAlive, so once built for a tab its navigation listener keeps running
/// for that tab's lifetime; this just guarantees it gets built when a tab
/// becomes selected (e.g. after opening a ruled site from the address bar).
@Riverpod(keepAlive: true)
void desktopModeRuleApplier(Ref ref) {
final selectedTabId = ref.watch(selectedTabProvider);
if (selectedTabId != null) {
ref.watch(desktopModeProvider(selectedTabId));
}
}
@@ -57,7 +57,7 @@ final class DesktopModeProvider extends $NotifierProvider<DesktopMode, bool> {
}
}
String _$desktopModeHash() => r'727c8a884de5c21499f4b3ae6835af1115f649f2';
String _$desktopModeHash() => r'e755b0a36f7079006047896cb8f2a7be484dea90';
final class DesktopModeFamily extends $Family
with $ClassFamilyOverride<DesktopMode, bool, bool, bool, String> {
@@ -97,3 +97,65 @@ abstract class _$DesktopMode extends $Notifier<bool> {
element.handleCreate(ref, () => build(_$args));
}
}
/// Always-mounted helper that instantiates the selected tab's [DesktopMode]
/// notifier so the per-site desktop-mode rule is applied on navigation even when
/// no tab menu or site sheet is open. Mounted by the browser view. [DesktopMode]
/// is keepAlive, so once built for a tab its navigation listener keeps running
/// for that tab's lifetime; this just guarantees it gets built when a tab
/// becomes selected (e.g. after opening a ruled site from the address bar).
@ProviderFor(desktopModeRuleApplier)
final desktopModeRuleApplierProvider = DesktopModeRuleApplierProvider._();
/// Always-mounted helper that instantiates the selected tab's [DesktopMode]
/// notifier so the per-site desktop-mode rule is applied on navigation even when
/// no tab menu or site sheet is open. Mounted by the browser view. [DesktopMode]
/// is keepAlive, so once built for a tab its navigation listener keeps running
/// for that tab's lifetime; this just guarantees it gets built when a tab
/// becomes selected (e.g. after opening a ruled site from the address bar).
final class DesktopModeRuleApplierProvider
extends $FunctionalProvider<void, void, void>
with $Provider<void> {
/// Always-mounted helper that instantiates the selected tab's [DesktopMode]
/// notifier so the per-site desktop-mode rule is applied on navigation even when
/// no tab menu or site sheet is open. Mounted by the browser view. [DesktopMode]
/// is keepAlive, so once built for a tab its navigation listener keeps running
/// for that tab's lifetime; this just guarantees it gets built when a tab
/// becomes selected (e.g. after opening a ruled site from the address bar).
DesktopModeRuleApplierProvider._()
: super(
from: null,
argument: null,
retry: null,
name: r'desktopModeRuleApplierProvider',
isAutoDispose: false,
dependencies: null,
$allTransitiveDependencies: null,
);
@override
String debugGetCreateSourceHash() => _$desktopModeRuleApplierHash();
@$internal
@override
$ProviderElement<void> $createElement($ProviderPointer pointer) =>
$ProviderElement(pointer);
@override
void create(Ref ref) {
return desktopModeRuleApplier(ref);
}
/// {@macro riverpod.override_with_value}
Override overrideWithValue(void value) {
return $ProviderOverride(
origin: this,
providerOverride: $SyncValueProvider<void>(value),
);
}
}
String _$desktopModeRuleApplierHash() =>
r'4a99102da5dc11869bfb96d439cc88652a190b39';
@@ -44,7 +44,7 @@ final class EngineSettingsReplicationServiceProvider
}
String _$engineSettingsReplicationServiceHash() =>
r'36f43a382419203ec53cac71fe51b1d30df2a0c6';
r'00439944023e8336dc879c70bb578120e221676d';
abstract class _$EngineSettingsReplicationService extends $Notifier<void> {
void build();
@@ -36,6 +36,7 @@ import 'package:weblibre/features/bangs/domain/services/search_history_cleanup.d
import 'package:weblibre/features/geckoview/domain/entities/tab_container_selection.dart';
import 'package:weblibre/features/geckoview/domain/providers.dart';
import 'package:weblibre/features/geckoview/domain/providers/browser_extension.dart';
import 'package:weblibre/features/geckoview/domain/providers/desktop_mode.dart';
import 'package:weblibre/features/geckoview/domain/providers/tab_session.dart';
import 'package:weblibre/features/geckoview/domain/providers/tab_state.dart';
import 'package:weblibre/features/geckoview/domain/providers/web_extensions_state.dart';
@@ -613,6 +614,19 @@ class _BrowserViewState extends ConsumerState<BrowserView>
},
);
ref.listenManual(
fireImmediately: true,
desktopModeRuleApplierProvider,
(previous, next) {},
onError: (error, stackTrace) {
logger.e(
'Error listening to desktopModeRuleApplierProvider',
error: error,
stackTrace: stackTrace,
);
},
);
ref.listenManual(
fireImmediately: true,
gestureControlServiceProvider,
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
import 'package:flutter_material_design_icons/flutter_material_design_icons.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:weblibre/core/logger.dart';
import 'package:weblibre/features/geckoview/domain/providers/desktop_mode.dart';
import 'package:weblibre/features/user/data/models/general_settings.dart';
import 'package:weblibre/features/user/domain/repositories/general_settings.dart';
import 'package:weblibre/utils/host_rules.dart';
import 'package:weblibre/utils/ui_helper.dart';
/// Section widget toggling whether the current site should always load in
/// desktop mode. Adds/removes the current host from the persisted rule list and
/// immediately reflects the change on the current tab.
class DesktopModeSection extends HookConsumerWidget {
final String tabId;
final Uri url;
const DesktopModeSection({required this.tabId, required this.url, super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final desktopModeSites = ref.watch(
generalSettingsWithDefaultsProvider.select((s) => s.desktopModeSites),
);
final host = normalizeRuleHost(url.toString());
final isRuled = hostMatchesRule(url, desktopModeSites);
// A broader entry (e.g. `example.com` while on `m.example.com`) governs this
// page; a per-site toggle for the exact host can't override it.
final parentRule = coveringParentRule(url, desktopModeSites);
return SwitchListTile.adaptive(
value: isRuled,
// Disabled when no valid host, or when a broader rule governs the page
// (which the exact-host toggle could not override).
onChanged: (host != null && parentRule == null)
? (enabled) => _toggleRule(context, ref, host, enabled)
: null,
title: const Text('Always use desktop site'),
subtitle: Text(
host == null
? 'Unavailable on this page'
: parentRule != null
? 'Set by a rule for $parentRule'
: isRuled
? 'This site always loads in desktop mode'
: 'This site follows the default mode',
),
secondary: Icon(
MdiIcons.monitor,
color: isRuled
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.onSurfaceVariant,
),
);
}
Future<void> _toggleRule(
BuildContext context,
WidgetRef ref,
String host,
bool enabled,
) async {
try {
await ref.read(generalSettingsRepositoryProvider.notifier).updateSettings(
(current) {
final next = current.desktopModeSites.toList();
if (enabled) {
if (!next.contains(host)) next.add(host);
} else {
next.remove(host);
}
return current.copyWith.desktopModeSites(next);
},
);
// Adding/removing a rule does not change the tab's host, so the
// host-change listener in DesktopMode won't fire. Apply the resolved
// state to the current tab directly: a new rule forces desktop on, while
// removing it reverts to the browser-wide default.
final globalDesktopMode = ref
.read(generalSettingsWithDefaultsProvider)
.globalDesktopMode;
ref
.read(desktopModeProvider(tabId).notifier)
.enabled(enabled || globalDesktopMode);
} catch (e, s) {
logger.e('Failed to toggle desktop mode rule', error: e, stackTrace: s);
if (context.mounted) {
showErrorMessage(context, 'Failed to toggle desktop mode: $e');
}
}
}
}
@@ -0,0 +1,113 @@
/*
* Copyright (c) 2024-2026 Fabian Freund.
*
* This file is part of WebLibre
* (see https://weblibre.eu).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:weblibre/core/logger.dart';
import 'package:weblibre/features/gestures/data/models/gesture_settings.dart';
import 'package:weblibre/features/gestures/domain/repositories/gesture_settings.dart';
import 'package:weblibre/utils/host_rules.dart';
import 'package:weblibre/utils/ui_helper.dart';
/// Section widget toggling whether touch gestures are enabled on the current
/// site. Mirrors the excluded-sites list managed in settings, but scoped to the
/// page currently shown in the sheet.
class GestureExclusionSection extends HookConsumerWidget {
final Uri url;
const GestureExclusionSection({required this.url, super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final masterEnabled = ref.watch(
gestureSettingsWithDefaultsProvider.select((s) => s.enabled),
);
final excludedSites = ref.watch(
gestureSettingsWithDefaultsProvider.select((s) => s.excludedSites),
);
final host = normalizeRuleHost(url.toString());
final isExcluded = hostMatchesRule(url, excludedSites);
// A broader entry (e.g. `example.com` while on `m.example.com`) governs this
// page; a per-site toggle for the exact host can't override it.
final parentRule = coveringParentRule(url, excludedSites);
// Enabled on this site when gestures are globally on, the host is valid,
// and the host is not in the exclusion list.
final isEnabledHere = masterEnabled && host != null && !isExcluded;
final String subtitle;
if (!masterEnabled) {
subtitle = 'Gestures are turned off globally';
} else if (host == null) {
subtitle = 'Gestures are unavailable on this page';
} else if (parentRule != null) {
subtitle = 'Disabled by a rule for $parentRule';
} else if (isExcluded) {
subtitle = 'Gestures are disabled on this site';
} else {
subtitle = 'Gestures are enabled on this site';
}
return SwitchListTile.adaptive(
value: isEnabledHere,
// Only actionable when gestures are globally enabled, we have a valid host
// to add to / remove from the exclusion list, and no broader rule governs
// the page (which the exact-host toggle could not override).
onChanged: (masterEnabled && host != null && parentRule == null)
? (enabled) => _toggleExclusion(context, ref, host, enabled)
: null,
title: const Text('Gestures'),
subtitle: Text(subtitle),
secondary: Icon(
isEnabledHere ? Icons.gesture : Icons.do_not_touch_outlined,
color: isEnabledHere
? Theme.of(context).colorScheme.primary
: Theme.of(context).colorScheme.onSurfaceVariant,
),
);
}
Future<void> _toggleExclusion(
BuildContext context,
WidgetRef ref,
String host,
bool enabled,
) async {
try {
await ref.read(gestureSettingsRepositoryProvider.notifier).updateSettings(
(current) {
final next = current.excludedSites.toList();
if (enabled) {
// Enabling gestures here => remove the host from the exclusion list.
next.remove(host);
} else if (!next.contains(host)) {
// Disabling gestures here => add the host to the exclusion list.
next.add(host);
}
return current.copyWith.excludedSites(next);
},
);
} catch (e, s) {
logger.e('Failed to toggle gesture exclusion', error: e, stackTrace: s);
if (context.mounted) {
showErrorMessage(context, 'Failed to toggle gestures: $e');
}
}
}
}
@@ -27,6 +27,8 @@ import 'package:weblibre/features/geckoview/domain/entities/states/tab.dart';
import 'package:weblibre/features/geckoview/features/browser/presentation/widgets/certificate_tile.dart';
import 'package:weblibre/features/geckoview/features/browser/presentation/widgets/draggable_scrollable_header.dart';
import 'package:weblibre/features/geckoview/features/browser/presentation/widgets/sheets/clear_site_data_section.dart';
import 'package:weblibre/features/geckoview/features/browser/presentation/widgets/sheets/desktop_mode_section.dart';
import 'package:weblibre/features/geckoview/features/browser/presentation/widgets/sheets/gesture_exclusion_section.dart';
import 'package:weblibre/features/geckoview/features/browser/presentation/widgets/sheets/permissions_section.dart';
import 'package:weblibre/features/geckoview/features/browser/presentation/widgets/sheets/tracking_protection_section.dart';
import 'package:weblibre/features/geckoview/features/tabs/data/entities/tab_mode.dart';
@@ -153,6 +155,15 @@ class ViewTabSheetWidget extends HookConsumerWidget {
// Tracking Protection Section
TrackingProtectionSection(tabId: initialTabState.id),
const Divider(),
// Gesture Exclusion Section
GestureExclusionSection(url: initialTabState.url),
const Divider(),
// Desktop Mode Section
DesktopModeSection(
tabId: initialTabState.id,
url: initialTabState.url,
),
const Divider(),
// Permissions Section
PermissionsSection(
origin: initialTabState.url.origin,