Skip to content
Draft
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
14 changes: 14 additions & 0 deletions example/lib/example.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class _MasterDetailPage extends StatelessWidget {
),
),
appBar: YaruWindowTitleBar(
style: getStyle(context),
title: const Text('Yaru'),
border: BorderSide.none,
backgroundColor: YaruMasterDetailTheme.of(context).sideBarColor,
Expand All @@ -88,6 +89,12 @@ class _MasterDetailPage extends StatelessWidget {
),
);
}

YaruTitleBarStyle getStyle(BuildContext context) {
return YaruTheme.maybeOf(context)?.hasLeftWindowControls == true
? YaruTitleBarStyle.onlyLeftWindowControls
: YaruTitleBarStyle.undecorated;
}
}

class _CompactPage extends StatefulWidget {
Expand Down Expand Up @@ -126,6 +133,7 @@ class _CompactPageState extends State<_CompactPage> {
leading: buildLeading(context, widget.pageItems[value]),
title: buildTitle(context, widget.pageItems[value]),
actions: buildActions(context, widget.pageItems[value]),
style: getStyle(context),
);
},
),
Expand Down Expand Up @@ -159,6 +167,12 @@ class _CompactPageState extends State<_CompactPage> {
}
}

YaruTitleBarStyle getStyle(BuildContext context) {
return YaruTheme.maybeOf(context)?.hasLeftWindowControls == true
? YaruTitleBarStyle.onlyLeftWindowControls
: YaruTitleBarStyle.normal;
}

void showSettingsDialog(BuildContext context) {
showDialog(context: context, builder: (context) => const SettingsDialog());
}
Expand Down
2 changes: 1 addition & 1 deletion example/lib/pages/search_field_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class _SearchFieldPageState extends State<SearchFieldPage> {
shadowColor: light ? Colors.black : null,
titlePadding: EdgeInsets.zero,
title: YaruDialogTitleBar(
heroTag: 'bar2',
rightHeroTag: 'bar2',
titleSpacing: 0,
centerTitle: true,
title: _fieldSearchActive
Expand Down
2 changes: 2 additions & 0 deletions lib/src/settings/gtk_constants.dart
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
const kSchemaInterface = 'org.gnome.desktop.interface';
const kSchemeWmPreferences = 'org.gnome.desktop.wm.preferences';
const kAccentColorKey = 'accent-color';
const kButtonLayoutKey = 'button-layout';
85 changes: 68 additions & 17 deletions lib/src/settings/inherited_theme.dart
Original file line number Diff line number Diff line change
Expand Up @@ -142,36 +142,49 @@ class YaruTheme extends StatefulWidget {
class _YaruThemeState extends State<YaruTheme> {
YaruVariant? _variant;
YaruSettings? _settings;
StreamSubscription<String?>? _themeNameSubscription;
StreamSubscription<String?>? _accentColorSubScription;
List<String>? _leftButtonLayout;
List<String>? _rightButtonLayout;
StreamSubscription<String?>? _themeNameSub;
StreamSubscription<String?>? _accentColorSub;
StreamSubscription<String?>? _buttonLayoutSub;

@override
void initState() {
super.initState();
if (widget.data.variant == null && canDetectVariant()) {
if (canDetectGnome()) {
_settings = widget._settings ?? YaruSettings();
_settings?.init();
_variant =
resolveAccentColorVariant(_settings?.getAccentColor()) ??
resolveGtkThemeVariant(_settings?.getThemeName());
_accentColorSubScription ??= _settings!.accentColorChanged.listen(
updateVariant,
);
_themeNameSubscription ??= _settings!.themeNameChanged.listen(
updateVariant,
if (widget.data.variant == null) {
_variant =
resolveAccentColorVariant(_settings?.getAccentColor()) ??
resolveGtkThemeVariant(_settings?.getThemeName());
_accentColorSub ??= _settings!.accentColorChanged.listen(updateVariant);
_themeNameSub ??= _settings!.themeNameChanged.listen(updateVariant);
}

_buttonLayoutSub ??= _settings!.buttonLayoutChanged.listen(
updateButtonLayout,
);

final buttonLayout = _settings?.getButtonLayout();

final buttons = buttonLayout?.split(':');

_leftButtonLayout = buttons?.firstOrNull?.split(',') ?? <String>[];
_rightButtonLayout = buttons?.lastOrNull?.split(',') ?? [];
}
}

@override
void dispose() {
_themeNameSubscription?.cancel();
_accentColorSubScription?.cancel();
_themeNameSub?.cancel();
_accentColorSub?.cancel();
_buttonLayoutSub?.cancel();
_settings?.dispose();
super.dispose();
}

bool canDetectVariant() {
bool canDetectGnome() {
return !kIsWeb &&
widget._platform.isLinux &&
!widget._platform.environment.containsKey('FLUTTER_TEST');
Expand Down Expand Up @@ -224,7 +237,7 @@ class _YaruThemeState extends State<YaruTheme> {
};

void updateVariant([String? value]) {
assert(canDetectVariant());
assert(canDetectGnome());
final gtkThemeName = value ?? _settings?.getThemeName();
final accentColor = value ?? _settings?.getAccentColor();
setState(
Expand All @@ -234,6 +247,18 @@ class _YaruThemeState extends State<YaruTheme> {
);
}

void updateButtonLayout([String? value]) {
assert(canDetectGnome());
final buttonLayout = value ?? _settings?.getButtonLayout();

final buttons = buttonLayout?.split(':');

setState(() {
_leftButtonLayout = buttons?.firstOrNull?.split(',') ?? <String>[];
_rightButtonLayout = buttons?.lastOrNull?.split(',') ?? [];
});
}

ThemeMode resolveMode() {
final mode = widget.data.themeMode ?? ThemeMode.system;
if (mode == ThemeMode.system) {
Expand All @@ -250,6 +275,8 @@ class _YaruThemeState extends State<YaruTheme> {
highContrast:
widget.data.highContrast ?? MediaQuery.highContrastOf(context),
themeMode: resolveMode(),
leftButtonLayout: widget.data.leftButtonLayout ?? _leftButtonLayout,
rightButtonLayout: widget.data.rightButtonLayout ?? _rightButtonLayout,
);
}

Expand All @@ -266,7 +293,7 @@ class _YaruThemeState extends State<YaruTheme> {

@override
Widget build(BuildContext context) {
if (_settings != null && _themeNameSubscription == null) {
if (_settings != null && _themeNameSub == null) {
return const SizedBox.shrink(); // #231
}
final data = resolveData();
Expand All @@ -289,11 +316,25 @@ class YaruThemeData with Diagnosticable {
this.pageTransitionsTheme,
this.useMaterial3,
this.visualDensity,
this.leftButtonLayout,
this.rightButtonLayout,
});

/// Specifies the theme variant.
final YaruVariant? variant;

/// If different from the default in GNOME, the title buttons on the left of the window.
final List<String>? leftButtonLayout;

/// If different from the default in GNOME, the title buttons on the left of the window.
final List<String>? rightButtonLayout;

/// If different from the default in GNOME, indicates if there are title buttons on the left of the window.
bool get hasLeftWindowControls => leftButtonLayout?.isNotEmpty ?? false;

/// If different from the default in GNOME, indicates if there are title buttons on the left of the window.
bool get hasRightWindowControls => rightButtonLayout?.isNotEmpty ?? false;

/// Whether to use high contrast colors.
final bool? highContrast;

Expand Down Expand Up @@ -330,6 +371,8 @@ class YaruThemeData with Diagnosticable {
PageTransitionsTheme? pageTransitionsTheme,
bool? useMaterial3,
VisualDensity? visualDensity,
List<String>? leftButtonLayout,
List<String>? rightButtonLayout,
}) {
return YaruThemeData(
variant: variant ?? this.variant,
Expand All @@ -339,6 +382,8 @@ class YaruThemeData with Diagnosticable {
pageTransitionsTheme: pageTransitionsTheme ?? this.pageTransitionsTheme,
useMaterial3: useMaterial3 ?? this.useMaterial3,
visualDensity: visualDensity ?? this.visualDensity,
leftButtonLayout: leftButtonLayout ?? this.leftButtonLayout,
rightButtonLayout: rightButtonLayout ?? this.rightButtonLayout,
);
}

Expand All @@ -354,6 +399,8 @@ class YaruThemeData with Diagnosticable {
);
properties.add(DiagnosticsProperty('useMaterial3', useMaterial3));
properties.add(DiagnosticsProperty('visualDensity', visualDensity));
properties.add(DiagnosticsProperty('leftButtonLayout', leftButtonLayout));
properties.add(DiagnosticsProperty('rightButtonLayout', rightButtonLayout));
}

@override
Expand All @@ -367,7 +414,9 @@ class YaruThemeData with Diagnosticable {
iterableEquals(other.extensions, extensions) &&
other.pageTransitionsTheme == pageTransitionsTheme &&
other.useMaterial3 == useMaterial3 &&
other.visualDensity == visualDensity;
other.visualDensity == visualDensity &&
other.leftButtonLayout == leftButtonLayout &&
other.rightButtonLayout == rightButtonLayout;
}

@override
Expand All @@ -380,6 +429,8 @@ class YaruThemeData with Diagnosticable {
pageTransitionsTheme,
useMaterial3,
visualDensity,
leftButtonLayout,
rightButtonLayout,
);
}
}
Expand Down
32 changes: 28 additions & 4 deletions lib/src/settings/yaru_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@ abstract class YaruSettings {

String? getThemeName();
String? getAccentColor();
String? getButtonLayout();
Stream<String?> get themeNameChanged;
Stream<String?> get accentColorChanged;
Stream<String?> get buttonLayoutChanged;
void init();
Future<void> dispose();
}
Expand All @@ -27,7 +29,8 @@ class YaruGtkSettings extends YaruSettings {

final GtkSettings _gtkSettings;
final GSettingsService _gSettingsService;
GnomeSettings? _gSettings;
GnomeSettings? _interfaceSettings;
GnomeSettings? _wmPrefSettings;

@override
String? getThemeName() => _gtkSettings.getProperty(kGtkThemeName) as String?;
Expand All @@ -40,18 +43,39 @@ class YaruGtkSettings extends YaruSettings {
@override
Stream<String?> get accentColorChanged => _accentColorController.stream;

final _buttonLayoutController = StreamController<String?>.broadcast();
@override
Stream<String?> get buttonLayoutChanged => _buttonLayoutController.stream;

@override
void init() {
_gSettings ??= _gSettingsService.lookup(kSchemaInterface);
_gSettings?.addListener(() => _accentColorController.add(getAccentColor()));
_interfaceSettings ??= _gSettingsService.lookup(kSchemaInterface);
_interfaceSettings?.addListener(
() => _accentColorController.add(getAccentColor()),
);
_wmPrefSettings ??= _gSettingsService.lookup(kSchemeWmPreferences);
_wmPrefSettings?.addListener(
() => _buttonLayoutController.add(getButtonLayout()),
);
}

@override
Future<void> dispose() async {
await _accentColorController.close();
await _buttonLayoutController.close();
await _gSettingsService.dispose();
}

@override
String? getAccentColor() => _gSettings?.stringValue(kAccentColorKey);
String? getAccentColor() => _interfaceSettings?.stringValue(kAccentColorKey);

@override
String? getButtonLayout() {
final layout = _wmPrefSettings?.stringValue(kButtonLayoutKey);
return (layout == null || layout == 'appmenu:close')
? _defaultLayout
: layout;
}
}

const _defaultLayout = ':minimize,maximize,close';
Loading