-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: follow accent-color gsettings key for 24.10 (#922)
* feat: follow accent-color gsettings key for 24.10 * fix: make theme sync work * fix: use switch expression
- Loading branch information
1 parent
2465f72
commit c88fe55
Showing
6 changed files
with
196 additions
and
33 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
const kSchemaInterface = 'org.gnome.desktop.interface'; | ||
const kAccentColorKey = 'accent-color'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import 'package:dbus/dbus.dart'; | ||
import 'package:flutter/foundation.dart'; | ||
import 'package:gsettings/gsettings.dart'; | ||
|
||
class GSettingsService { | ||
final _settings = <String, GnomeSettings?>{}; | ||
|
||
GnomeSettings? lookup(String schemaId, {String? path}) { | ||
try { | ||
return _settings[schemaId] ??= GnomeSettings(schemaId, path: path); | ||
} on GSettingsSchemaNotInstalledException catch (_) { | ||
return null; | ||
} | ||
} | ||
|
||
void dispose() { | ||
for (final settings in _settings.values) { | ||
settings?.dispose(); | ||
} | ||
} | ||
} | ||
|
||
class GnomeSettings { | ||
GnomeSettings(String schemaId, {String? path}) | ||
: _settings = GSettings(schemaId, path: path) { | ||
_settings.keysChanged.listen((keys) { | ||
for (final key in keys) { | ||
_updateValue(key); | ||
} | ||
}); | ||
} | ||
|
||
final GSettings _settings; | ||
final _values = <String, dynamic>{}; | ||
final _listeners = <VoidCallback>{}; | ||
|
||
void addListener(VoidCallback listener) => _listeners.add(listener); | ||
void removeListener(VoidCallback listener) => _listeners.remove(listener); | ||
void notifyListeners() { | ||
for (final listener in _listeners) { | ||
listener(); | ||
} | ||
} | ||
|
||
void dispose() => _settings.close(); | ||
|
||
bool? boolValue(String key) => getValue<bool>(key); | ||
int? intValue(String key) => getValue<int>(key); | ||
double? doubleValue(String key) => getValue<double>(key); | ||
String? stringValue(String key) => getValue<String>(key); | ||
Iterable<String>? stringArrayValue(String key) => | ||
getValue<Iterable>(key)?.cast<String>(); | ||
|
||
T? getValue<T>(String key) => _values[key] ?? _updateValue(key); | ||
|
||
T? _updateValue<T>(String key) { | ||
T? value; | ||
try { | ||
_settings.get(key).then((v) { | ||
value = v.toNative() as T?; | ||
if (_values[key] != value) { | ||
_values[key] = value; | ||
notifyListeners(); | ||
} | ||
}); | ||
} on GSettingsUnknownKeyException catch (_) {} | ||
return value; | ||
} | ||
|
||
Future<void> setValue<T>(String key, T value) async { | ||
if (_values[key] == value) return; | ||
_values[key] = value; | ||
|
||
return switch (T) { | ||
const (bool) => _settings.set(key, DBusBoolean(value as bool)), | ||
const (int) => _settings.set(key, DBusInt32(value as int)), | ||
const (double) => _settings.set(key, DBusDouble(value as double)), | ||
const (String) => _settings.set(key, DBusString(value as String)), | ||
const (List<String>) => | ||
_settings.set(key, DBusArray.string(value as List<String>)), | ||
_ => throw UnsupportedError('Unsupported type: $T'), | ||
}; | ||
} | ||
|
||
Future<void> setUint32Value(String key, int value) async { | ||
if (_values[key] == value) return; | ||
_values[key] = value; | ||
await _settings.set(key, DBusUint32(value)); | ||
} | ||
|
||
Future<void> resetValue(String key) => | ||
_settings.setAll(<String, DBusValue?>{key: null}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import 'dart:async'; | ||
|
||
import 'package:flutter/foundation.dart'; | ||
import 'package:gtk/gtk.dart'; | ||
import 'package:yaru/src/theme_widgets/gtk_constants.dart'; | ||
import 'package:yaru/src/theme_widgets/settings_service.dart'; | ||
|
||
abstract class YaruSettings { | ||
factory YaruSettings() = YaruGtkSettings; | ||
const YaruSettings._(); | ||
|
||
String? getThemeName(); | ||
String? getAccentColor(); | ||
Stream<String?> get themeNameChanged; | ||
Stream<String?> get accentColorChanged; | ||
void init(); | ||
Future<void> dispose(); | ||
} | ||
|
||
class YaruGtkSettings extends YaruSettings { | ||
YaruGtkSettings([ | ||
@visibleForTesting GtkSettings? settings, | ||
@visibleForTesting GSettingsService? settingsService, | ||
]) : _gtkSettings = settings ?? GtkSettings(), | ||
_gSettingsService = settingsService ?? GSettingsService(), | ||
super._(); | ||
|
||
final GtkSettings _gtkSettings; | ||
final GSettingsService _gSettingsService; | ||
GnomeSettings? _gSettings; | ||
|
||
@override | ||
String? getThemeName() => _gtkSettings.getProperty(kGtkThemeName) as String?; | ||
|
||
@override | ||
Stream<String?> get themeNameChanged => | ||
_gtkSettings.notifyProperty(kGtkThemeName).cast<String?>(); | ||
|
||
final _accentColorController = StreamController<String?>.broadcast(); | ||
@override | ||
Stream<String?> get accentColorChanged => _accentColorController.stream; | ||
|
||
@override | ||
void init() { | ||
_gSettings ??= _gSettingsService.lookup(kSchemaInterface); | ||
_gSettings?.addListener( | ||
() => _accentColorController.add(getAccentColor()), | ||
); | ||
} | ||
|
||
@override | ||
Future<void> dispose() async { | ||
await _accentColorController.close(); | ||
_gSettingsService.dispose(); | ||
} | ||
|
||
@override | ||
String? getAccentColor() => _gSettings?.stringValue(kAccentColorKey); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters