Repository navigation
Let the desktop app scale its text up and down #1677
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b829217
Let the desktop app scale its text up and down
realtonyyoung 12c3033
Release a closed main window from the UI scale
realtonyyoung 9f35837
Keep the title bar and window minimum unscaled under zoom
realtonyyoung 054ad77
Recompute the zoomed window minimum when the window changes screen
realtonyyoung 713d5f2
Keep onboarding unscaled and refresh the zoomed minimum on display ch…
realtonyyoung e7d741e
Release the screens on a real close of the zoomed window
realtonyyoung fc2942e
Drop a comment that restates the Appearance tab binding
realtonyyoung File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,33 @@ | ||
| using System.Globalization; | ||
|
|
||
| namespace Capacitor.App.Services; | ||
|
|
||
| /// How much the main window magnifies its content: text and the layout around it grow together, | ||
| /// because control font sizes are fixed per control. Kept to whole steps so a value read back from | ||
| /// app state lands on one the shortcuts can step from. | ||
| public static class UiScale { | ||
| public const double Default = 1.0; | ||
| public const double Min = 0.8; | ||
| public const double Max = 1.5; | ||
| const double Step = 0.1; | ||
| const int StepsPerUnit = 10; | ||
|
|
||
| public static double Normalize(double? scale) { | ||
| if (scale is not { } value || !double.IsFinite(value)) return Default; | ||
|
|
||
| // Multiplied rather than divided by the step: 0.95 / 0.1 lands just below 9.5 and would round down. | ||
| return Math.Round(Math.Round(Math.Clamp(value, Min, Max) * StepsPerUnit, MidpointRounding.AwayFromZero) / StepsPerUnit, 2); | ||
| } | ||
|
|
||
| public static double Larger(double scale) => Normalize(Normalize(scale) + Step); | ||
|
|
||
| public static double Smaller(double scale) => Normalize(Normalize(scale) - Step); | ||
|
|
||
| /// <summary>A window minimum that leaves the magnified content the room it is laid out for, without | ||
| /// growing past what the screen offers. Never below the unscaled minimum.</summary> | ||
| public static double Minimum(double unscaled, double scale, double? available) => | ||
| Math.Max(unscaled, Math.Min(unscaled * Normalize(scale), available ?? double.MaxValue)); | ||
|
|
||
| public static string Label(double scale) => | ||
| ((int)Math.Round(Normalize(scale) * 100)).ToString(CultureInfo.InvariantCulture) + "%"; | ||
| } |
This file contains hidden or 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,33 @@ | ||
| using System.Reactive.Subjects; | ||
|
|
||
| namespace Capacitor.App.Services; | ||
|
|
||
| /// The app's one UI scale, shared by the main window and Settings and kept in app state. | ||
| public sealed class UiScaleSetting { | ||
| readonly IAppStateStore _store; | ||
| readonly BehaviorSubject<double> _changes; | ||
|
|
||
| UiScaleSetting(IAppStateStore store, double initial) { | ||
| _store = store; | ||
| _changes = new BehaviorSubject<double>(initial); | ||
| } | ||
|
|
||
| public static async Task<UiScaleSetting> LoadAsync(IAppStateStore store) => | ||
| new(store, UiScale.Normalize((await store.LoadAsync().ConfigureAwait(false)).UiScale)); | ||
|
|
||
| public double Current => _changes.Value; | ||
|
|
||
| /// The current scale, then each change. | ||
| public IObservable<double> Changes => _changes; | ||
|
|
||
| /// A failed write keeps the scale for this run. Each write stores the scale current when it runs, | ||
| /// so overlapping changes cannot leave an older one on disk. | ||
| public Task SetAsync(double scale) { | ||
| var next = UiScale.Normalize(scale); | ||
| if (next == _changes.Value) return Task.CompletedTask; | ||
|
|
||
| _changes.OnNext(next); | ||
| // Off the caller's thread: an uncontended store reads and writes the file before its first await. | ||
| return Task.Run(() => _store.UpdateAsync(state => state with { UiScale = _changes.Value })); | ||
| } | ||
| } | ||
47 changes: 47 additions & 0 deletions
47
src/Capacitor.App/ViewModels/AppearanceSettingsViewModel.cs
This file contains hidden or 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,47 @@ | ||
| using System.Reactive; | ||
| using System.Reactive.Linq; | ||
| using Capacitor.App.Services; | ||
| using ReactiveUI.Reactive; | ||
|
|
||
| namespace Capacitor.App.ViewModels; | ||
|
|
||
| /// Settings' Appearance tab: the UI scale, which the main window's shortcuts can also change while | ||
| /// the tab is open. | ||
| public sealed class AppearanceSettingsViewModel : ReactiveObject, IDisposable { | ||
| readonly IDisposable _subscription; | ||
| double _scale; | ||
|
|
||
| public AppearanceSettingsViewModel(UiScaleSetting setting) { | ||
| _scale = setting.Current; | ||
| var scale = this.WhenAnyValue(x => x.Scale); | ||
| SmallerCommand = ReactiveCommand.CreateFromTask(() => setting.SetAsync(UiScale.Smaller(Scale)), scale.Select(s => s > UiScale.Min)); | ||
| LargerCommand = ReactiveCommand.CreateFromTask(() => setting.SetAsync(UiScale.Larger(Scale)), scale.Select(s => s < UiScale.Max)); | ||
| ResetCommand = ReactiveCommand.CreateFromTask(() => setting.SetAsync(UiScale.Default), scale.Select(s => s != UiScale.Default)); | ||
| _subscription = setting.Changes.ObserveOn(RxSchedulers.MainThreadScheduler).Subscribe(s => Scale = s); | ||
| } | ||
|
|
||
| public double Scale { | ||
| get => _scale; | ||
| private set { | ||
| this.RaiseAndSetIfChanged(ref _scale, value); | ||
| this.RaisePropertyChanged(nameof(ScaleLabel)); | ||
| } | ||
| } | ||
|
|
||
| public string ScaleLabel => UiScale.Label(Scale); | ||
|
|
||
| public string ShortcutHint { get; } = OperatingSystem.IsMacOS() | ||
| ? "Scales text and layout together. ⌘+ and ⌘− also change it." | ||
| : "Scales text and layout together. Ctrl++ and Ctrl+− also change it, and Ctrl+0 resets it."; | ||
|
|
||
| public ReactiveCommand<Unit, Unit> SmallerCommand { get; } | ||
| public ReactiveCommand<Unit, Unit> LargerCommand { get; } | ||
| public ReactiveCommand<Unit, Unit> ResetCommand { get; } | ||
|
|
||
| public void Dispose() { | ||
| _subscription.Dispose(); | ||
| SmallerCommand.Dispose(); | ||
| LargerCommand.Dispose(); | ||
| ResetCommand.Dispose(); | ||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.