From 0b353037bab4dfd792cd8791293d1ffe1460180e Mon Sep 17 00:00:00 2001 From: Tig Date: Tue, 8 Oct 2024 11:01:19 -0400 Subject: [PATCH] Fixed Theme bugs --- .../Application/Application.Initialization.cs | 5 ++ .../Configuration/ConfigurationManager.cs | 16 ++++-- Terminal.Gui/Configuration/ThemeManager.cs | 2 +- Terminal.Gui/View/Layout/DimAuto.cs | 42 ++++++++++++++ Terminal.Gui/Views/Bar.cs | 22 ++++---- UICatalog/Scenario.cs | 1 - UICatalog/Scenarios/Adornments.cs | 2 +- UICatalog/Scenarios/AllViewsTester.cs | 56 ++++++++++++------- UICatalog/Scenarios/CombiningMarks.cs | 2 - UICatalog/UICatalog.cs | 2 - 10 files changed, 107 insertions(+), 43 deletions(-) diff --git a/Terminal.Gui/Application/Application.Initialization.cs b/Terminal.Gui/Application/Application.Initialization.cs index 62822f3c2..4fb11621d 100644 --- a/Terminal.Gui/Application/Application.Initialization.cs +++ b/Terminal.Gui/Application/Application.Initialization.cs @@ -99,7 +99,12 @@ public static partial class Application // Initialization (Init/Shutdown) // valid after a Driver is loaded. In this case we need just // `Settings` so we can determine which driver to use. // Don't reset, so we can inherit the theme from the previous run. + string previousTheme = Themes?.Theme ?? string.Empty; Load (); + if (Themes is { } && !string.IsNullOrEmpty (previousTheme) && previousTheme != "Default") + { + ThemeManager.SelectedTheme = previousTheme; + } Apply (); AddApplicationKeyBindings (); diff --git a/Terminal.Gui/Configuration/ConfigurationManager.cs b/Terminal.Gui/Configuration/ConfigurationManager.cs index 0171fc844..dacb87891 100644 --- a/Terminal.Gui/Configuration/ConfigurationManager.cs +++ b/Terminal.Gui/Configuration/ConfigurationManager.cs @@ -197,10 +197,18 @@ public static class ConfigurationManager try { - settings = Settings?.Apply () ?? false; - - themes = !string.IsNullOrEmpty (ThemeManager.SelectedTheme) - && (ThemeManager.Themes? [ThemeManager.SelectedTheme]?.Apply () ?? false); + if (string.IsNullOrEmpty (ThemeManager.SelectedTheme)) + { + settings = Settings?.Apply () ?? false; + themes = !string.IsNullOrEmpty (ThemeManager.SelectedTheme) + && (ThemeManager.Themes? [ThemeManager.SelectedTheme]?.Apply () ?? false); + } + else + { + themes = !string.IsNullOrEmpty (ThemeManager.SelectedTheme) + && (ThemeManager.Themes? [ThemeManager.SelectedTheme]?.Apply () ?? false); + settings = Settings?.Apply () ?? false; + } appSettings = AppSettings?.Apply () ?? false; } catch (JsonException e) diff --git a/Terminal.Gui/Configuration/ThemeManager.cs b/Terminal.Gui/Configuration/ThemeManager.cs index daf2edd9d..b542c9f2c 100644 --- a/Terminal.Gui/Configuration/ThemeManager.cs +++ b/Terminal.Gui/Configuration/ThemeManager.cs @@ -110,7 +110,7 @@ public class ThemeManager : IDictionary string oldTheme = _theme; _theme = value; - if (oldTheme != _theme && Settings! ["Themes"]?.PropertyValue is Dictionary themes && themes.ContainsKey (_theme)) + if ((oldTheme != _theme || oldTheme != Settings! ["Theme"].PropertyValue as string) && Settings! ["Themes"]?.PropertyValue is Dictionary themes && themes.ContainsKey (_theme)) { Settings! ["Theme"].PropertyValue = _theme; Instance.OnThemeChanged (oldTheme); diff --git a/Terminal.Gui/View/Layout/DimAuto.cs b/Terminal.Gui/View/Layout/DimAuto.cs index a2061416d..f159b6476 100644 --- a/Terminal.Gui/View/Layout/DimAuto.cs +++ b/Terminal.Gui/View/Layout/DimAuto.cs @@ -408,6 +408,48 @@ public record DimAuto (Dim? MaximumContentDim, Dim? MinimumContentDim, DimAutoSt } #endregion DimView + + #region DimAuto + // [ ] DimAuto - Dimension is internally calculated + + List dimAutoSubViews; + + if (dimension == Dimension.Width && us.GetType ().Name == "Bar" && us.Subviews.Count == 3) + { + + } + + if (dimension == Dimension.Width) + { + dimAutoSubViews = includedSubviews.Where (v => v.Width is { } && v.Width.Has (out _)).ToList (); + } + else + { + dimAutoSubViews = includedSubviews.Where (v => v.Height is { } && v.Height.Has (out _)).ToList (); + } + + for (var i = 0; i < dimAutoSubViews.Count; i++) + { + View v = dimAutoSubViews [i]; + + if (dimension == Dimension.Width) + { + v.SetRelativeLayout (new (maxCalculatedSize, 0)); + } + else + { + v.SetRelativeLayout (new (0, maxCalculatedSize)); + } + + int maxDimAuto= dimension == Dimension.Width ? v.Frame.X + v.Frame.Width : v.Frame.Y + v.Frame.Height; + + if (maxDimAuto > maxCalculatedSize) + { + maxCalculatedSize = maxDimAuto; + } + } + + #endregion } } diff --git a/Terminal.Gui/Views/Bar.cs b/Terminal.Gui/Views/Bar.cs index 266078f5f..ddaa44436 100644 --- a/Terminal.Gui/Views/Bar.cs +++ b/Terminal.Gui/Views/Bar.cs @@ -77,7 +77,11 @@ public class Bar : View, IOrientation, IDesignable } } - private void Bar_Initialized (object? sender, EventArgs e) { ColorScheme = Colors.ColorSchemes ["Menu"]; } + private void Bar_Initialized (object? sender, EventArgs e) + { + ColorScheme = Colors.ColorSchemes ["Menu"]; + LayoutBarItems (GetContentSize ()); + } /// public override void SetBorderStyle (LineStyle value) @@ -192,6 +196,11 @@ public class Bar : View, IOrientation, IDesignable { base.OnLayoutStarted (args); + LayoutBarItems (args.OldContentSize); + } + + private void LayoutBarItems (Size contentSize) + { View? prevBarItem = null; switch (Orientation) @@ -204,8 +213,6 @@ public class Bar : View, IOrientation, IDesignable barItem.ColorScheme = ColorScheme; barItem.X = Pos.Align (Alignment.Start, AlignmentModes); barItem.Y = 0; //Pos.Center (); - // HACK: This should not be needed - barItem.SetRelativeLayout (GetContentSize ()); } break; @@ -239,8 +246,6 @@ public class Bar : View, IOrientation, IDesignable if (barItem is Shortcut scBarItem) { scBarItem.MinimumKeyTextSize = minKeyWidth; - // HACK: This should not be needed - scBarItem.SetRelativeLayout (GetContentSize ()); maxBarItemWidth = Math.Max (maxBarItemWidth, scBarItem.Frame.Width); } @@ -264,10 +269,6 @@ public class Bar : View, IOrientation, IDesignable foreach (View barItem in Subviews) { barItem.Width = maxBarItemWidth; - - if (barItem is Line line) - { - } } Height = Dim.Auto (DimAutoStyle.Content, totalHeight); @@ -276,7 +277,6 @@ public class Bar : View, IOrientation, IDesignable } } - /// public bool EnableForDesign () { @@ -301,7 +301,7 @@ public class Bar : View, IOrientation, IDesignable shortcut = new Shortcut { Text = "Czech", - CommandView = new CheckBox() + CommandView = new CheckBox () { Title = "_Check" }, diff --git a/UICatalog/Scenario.cs b/UICatalog/Scenario.cs index b7277e764..f0a03f5ce 100644 --- a/UICatalog/Scenario.cs +++ b/UICatalog/Scenario.cs @@ -82,7 +82,6 @@ namespace UICatalog; public class Scenario : IDisposable { private static int _maxScenarioNameLen = 30; - public string Theme = "Default"; public string TopLevelColorScheme = "Base"; private bool _disposedValue; diff --git a/UICatalog/Scenarios/Adornments.cs b/UICatalog/Scenarios/Adornments.cs index 48e5211ef..d4d54b819 100644 --- a/UICatalog/Scenarios/Adornments.cs +++ b/UICatalog/Scenarios/Adornments.cs @@ -36,7 +36,7 @@ public class Adornments : Scenario // X = Pos.Center (), Width = Dim.Percent (60), - Height = Dim.Percent (80) + Height = Dim.Percent (90) }; app.Add (window); diff --git a/UICatalog/Scenarios/AllViewsTester.cs b/UICatalog/Scenarios/AllViewsTester.cs index d9635dd5c..c0d64051e 100644 --- a/UICatalog/Scenarios/AllViewsTester.cs +++ b/UICatalog/Scenarios/AllViewsTester.cs @@ -50,7 +50,7 @@ public class AllViewsTester : Scenario { // Don't create a sub-win (Scenario.Win); just use Application.Top Application.Init (); - ConfigurationManager.Apply (); + // ConfigurationManager.Apply (); var app = new Window { @@ -88,18 +88,10 @@ public class AllViewsTester : Scenario _classListView.SelectedItemChanged += (s, args) => { - // Remove existing class, if any - if (_curView != null) - { - _curView.LayoutComplete -= LayoutCompleteHandler; - _hostPane.Remove (_curView); - _curView.Dispose (); - _curView = null; - } + // Dispose existing current View, if any + DisposeCurrentView (); - _curView = CreateClass (_viewClasses.Values.ToArray () [_classListView.SelectedItem]); - // Add - _hostPane.Add (_curView); + CreateCurrentView (_viewClasses.Values.ToArray () [_classListView.SelectedItem]); // Force ViewToEdit to be the view and not a subview if (_adornmentsEditor is { }) @@ -326,6 +318,11 @@ public class AllViewsTester : Scenario ColorScheme = Colors.ColorSchemes ["Dialog"] }; + _hostPane.LayoutStarted += (sender, args) => + { + + }; + app.Add (_leftPane, _adornmentsEditor, _settingsPane, _hostPane); _classListView.SelectedItem = 0; @@ -345,8 +342,10 @@ public class AllViewsTester : Scenario private void OnXRadioGroupOnSelectedItemChanged (object s, SelectedItemChangedArgs selected) { DimPosChanged (_curView); } // TODO: Add Command.HotKey handler (pop a message box?) - private View CreateClass (Type type) + private void CreateCurrentView (Type type) { + Debug.Assert(_curView is null); + // If we are to create a generic Type if (type.IsGenericType) { @@ -386,9 +385,24 @@ public class AllViewsTester : Scenario _orientation.Enabled = false; } - view.Initialized += View_Initialized; + view.Initialized += CurrentView_Initialized; + view.LayoutComplete += CurrentView_LayoutComplete; - return view; + _curView = view; + _hostPane.Add (_curView); + // Application.Refresh(); + } + + private void DisposeCurrentView () + { + if (_curView != null) + { + _curView.Initialized -= CurrentView_Initialized; + _curView.LayoutComplete -= CurrentView_LayoutComplete; + _hostPane.Remove (_curView); + _curView.Dispose (); + _curView = null; + } } private void DimPosChanged (View view) @@ -463,7 +477,7 @@ public class AllViewsTester : Scenario _hText.Enabled = true; } - UpdateTitle (view); + UpdateHostTitle (view); } private List GetAllViewClassesCollection () @@ -484,10 +498,10 @@ public class AllViewsTester : Scenario return types; } - private void LayoutCompleteHandler (object sender, LayoutEventArgs args) + private void CurrentView_LayoutComplete (object sender, LayoutEventArgs args) { UpdateSettings (_curView); - UpdateTitle (_curView); + UpdateHostTitle (_curView); } private void UpdateSettings (View view) @@ -537,9 +551,9 @@ public class AllViewsTester : Scenario } } - private void UpdateTitle (View view) { _hostPane.Title = $"{view.GetType ().Name} - {view.X}, {view.Y}, {view.Width}, {view.Height}"; } + private void UpdateHostTitle (View view) { _hostPane.Title = $"_Demo of {view.GetType ().Name}"; } - private void View_Initialized (object sender, EventArgs e) + private void CurrentView_Initialized (object sender, EventArgs e) { if (sender is not View view) { @@ -566,6 +580,6 @@ public class AllViewsTester : Scenario _hRadioGroup.SelectedItemChanged += OnHRadioGroupOnSelectedItemChanged; _wRadioGroup.SelectedItemChanged += OnWRadioGroupOnSelectedItemChanged; - UpdateTitle (view); + UpdateHostTitle (view); } } diff --git a/UICatalog/Scenarios/CombiningMarks.cs b/UICatalog/Scenarios/CombiningMarks.cs index 78455eeca..3b2a8e196 100644 --- a/UICatalog/Scenarios/CombiningMarks.cs +++ b/UICatalog/Scenarios/CombiningMarks.cs @@ -9,8 +9,6 @@ public class CombiningMarks : Scenario public override void Main () { Application.Init (); - ConfigurationManager.Themes!.Theme = Theme; - ConfigurationManager.Apply (); var top = new Toplevel { ColorScheme = Colors.ColorSchemes [TopLevelColorScheme] }; top.DrawContentComplete += (s, e) => diff --git a/UICatalog/UICatalog.cs b/UICatalog/UICatalog.cs index 049f365b7..975fcc048 100644 --- a/UICatalog/UICatalog.cs +++ b/UICatalog/UICatalog.cs @@ -317,7 +317,6 @@ public class UICatalogApp _selectedScenario = (Scenario)Activator.CreateInstance (_scenarios [item].GetType ())!; Application.Init (driverName: _forceDriver); - _selectedScenario.Theme = _cachedTheme; _selectedScenario.TopLevelColorScheme = _topLevelColorScheme; _selectedScenario.Main (); _selectedScenario.Dispose (); @@ -335,7 +334,6 @@ public class UICatalogApp VerifyObjectsWereDisposed (); Themes!.Theme = _cachedTheme!; Apply (); - scenario.Theme = _cachedTheme; scenario.TopLevelColorScheme = _topLevelColorScheme; #if DEBUG_IDISPOSABLE