diff --git a/MacUtilGUI.Tests/TweakTabTests.fs b/MacUtilGUI.Tests/TweakTabTests.fs index 04c85d0..e14a21d 100644 --- a/MacUtilGUI.Tests/TweakTabTests.fs +++ b/MacUtilGUI.Tests/TweakTabTests.fs @@ -1,5 +1,7 @@ namespace MacUtilGUI.Tests +open System +open System.IO open Xunit open MacUtilGUI.Models open MacUtilGUI.Services @@ -24,6 +26,24 @@ module TweakTabTests = for row in vm.CautionTweaks do row.IsChecked <- false + let private repoRoot () = + let rec walk dir = + if String.IsNullOrEmpty dir then + None + elif File.Exists(Path.Combine(dir, "MacUtilGUI", "Views", "MainWindow.axaml")) then + Some dir + else + let parent = Directory.GetParent dir + + if isNull parent then None else walk parent.FullName + + match walk AppContext.BaseDirectory with + | Some dir -> dir + | None -> + match walk (Directory.GetCurrentDirectory()) with + | Some dir -> dir + | None -> failwith "repo root not found" + [] let TweakRowUncheckedWhenAbsent () = let client = FakeDefaultsClient() @@ -84,3 +104,78 @@ module TweakTabTests = Assert.True(client.Read("com.apple.finder", "ShowPathbar").IsNone) Assert.False(pathBar.IsChecked) Assert.True(ActionEngine.detect client extensions.Tweak) + + [] + let TweakGroupsByCategory () = + let client = FakeDefaultsClient() + let vm = makeVm client + + let safeNames = + vm.SafeTweakGroups |> Seq.map (fun group -> group.Name) |> Seq.toList + + Assert.Equal([ "Finder"; "Dock"; "Keyboard"; "Screenshots"; "Privacy" ], safeNames) + + let cautionNames = + vm.CautionTweakGroups |> Seq.map (fun group -> group.Name) |> Seq.toList + + Assert.Equal([ "Keyboard"; "Screenshots"; "Privacy" ], cautionNames) + + Assert.Equal(vm.SafeTweaks.Count, vm.SafeTweakGroups |> Seq.sumBy (fun group -> group.Tweaks.Count)) + Assert.Equal(vm.CautionTweaks.Count, vm.CautionTweakGroups |> Seq.sumBy (fun group -> group.Tweaks.Count)) + + Assert.False( + vm.CautionTweakGroups + |> Seq.exists (fun group -> group.Name = "Finder" || group.Name = "Dock") + ) + + let groupedSafe = + vm.SafeTweakGroups |> Seq.collect (fun group -> group.Tweaks) |> Seq.toList + + Assert.Equal(vm.SafeTweaks.Count, groupedSafe.Length) + + for row in vm.SafeTweaks do + Assert.Contains(row, groupedSafe) + + let finder = vm.SafeTweakGroups |> Seq.find (fun group -> group.Name = "Finder") + finder.AllSelected <- true + Assert.True(finder.Tweaks |> Seq.forall (fun row -> row.IsChecked)) + Assert.True(finder.AllSelected) + + for row in vm.CautionTweaks do + Assert.False(row.IsChecked) + + finder.AllSelected <- false + Assert.True(finder.Tweaks |> Seq.forall (fun row -> not row.IsChecked)) + Assert.False(finder.AllSelected) + + vm.SelectStandard() + let standard = Set.ofList (catalog ()).Presets.["Standard"] + + for row in vm.SafeTweaks do + Assert.Equal(standard.Contains row.Id, row.IsChecked) + + if row.IsChecked then + Assert.True(row.Category = "Finder" || row.Category = "Dock") + + for row in vm.CautionTweaks do + Assert.False(row.IsChecked) + + uncheckAll vm + let writesBefore = client.WriteCount + vm.ApplySelected() + Assert.Equal(writesBefore, client.WriteCount) + Assert.Equal("Nothing is selected.", vm.StatusText) + Assert.Empty(vm.SelectedIds) + + let axaml = + File.ReadAllText(Path.Combine(repoRoot (), "MacUtilGUI", "Views", "MainWindow.axaml")) + + Assert.Contains("SafeTweakGroups", axaml) + Assert.Contains("CautionTweakGroups", axaml) + Assert.Contains("TweakGroupViewModel", axaml) + Assert.DoesNotContain("ItemsSource=\"{Binding SafeTweaks}\"", axaml) + Assert.DoesNotContain("ItemsSource=\"{Binding CautionTweaks}\"", axaml) + Assert.Contains("ItemsSource=\"{Binding AppGroups}\"", axaml) + Assert.Contains("Maintenance", axaml) + Assert.Contains("Updates", axaml) + Assert.Contains("#1C1C1E", axaml) diff --git a/MacUtilGUI/MacUtilGUI.fsproj b/MacUtilGUI/MacUtilGUI.fsproj index 9566ce7..c95a463 100644 --- a/MacUtilGUI/MacUtilGUI.fsproj +++ b/MacUtilGUI/MacUtilGUI.fsproj @@ -52,6 +52,7 @@ + diff --git a/MacUtilGUI/ViewModels/MainWindowViewModel.fs b/MacUtilGUI/ViewModels/MainWindowViewModel.fs index 9e34bb4..4dbb78b 100644 --- a/MacUtilGUI/ViewModels/MainWindowViewModel.fs +++ b/MacUtilGUI/ViewModels/MainWindowViewModel.fs @@ -34,6 +34,8 @@ type MainWindowViewModel let mutable searchText: string = "" let safeTweaks = ObservableCollection() let cautionTweaks = ObservableCollection() + let safeTweakGroups = ObservableCollection() + let cautionTweakGroups = ObservableCollection() let allApps = ResizeArray() let apps = ObservableCollection() let appGroups = ObservableCollection() @@ -232,6 +234,12 @@ type MainWindowViewModel | "Privacy" -> 4 | _ -> 5 + let fillTweakGroups (source: TweakRowViewModel seq) (target: ObservableCollection) = + source + |> Seq.groupBy (fun (row: TweakRowViewModel) -> row.Category) + |> Seq.sortBy (fun (category, _) -> categoryRank category, category) + |> Seq.iter (fun (category, rows) -> target.Add(TweakGroupViewModel(category, rows))) + do catalog.Tweaks |> Map.toSeq @@ -244,6 +252,9 @@ type MainWindowViewModel | Risk.Safe -> safeTweaks.Add(row) | Risk.Caution -> cautionTweaks.Add(row)) + fillTweakGroups safeTweaks safeTweakGroups + fillTweakGroups cautionTweaks cautionTweakGroups + catalog.Apps |> Map.toSeq |> Seq.map snd @@ -277,6 +288,10 @@ type MainWindowViewModel member _.CautionTweaks = cautionTweaks + member _.SafeTweakGroups = safeTweakGroups + + member _.CautionTweakGroups = cautionTweakGroups + member _.Apps = apps member _.AppGroups = appGroups diff --git a/MacUtilGUI/ViewModels/TweakGroupViewModel.fs b/MacUtilGUI/ViewModels/TweakGroupViewModel.fs new file mode 100644 index 0000000..1d91b93 --- /dev/null +++ b/MacUtilGUI/ViewModels/TweakGroupViewModel.fs @@ -0,0 +1,22 @@ +namespace MacUtilGUI.ViewModels + +open System.Collections.ObjectModel + +type TweakGroupViewModel(name: string, rows: TweakRowViewModel seq) = + inherit ViewModelBase() + + let tweaks = ObservableCollection(rows) + + member _.Name = name + + member _.Tweaks = tweaks + + member this.AllSelected + with get () = + tweaks.Count > 0 + && Seq.forall (fun (row: TweakRowViewModel) -> row.IsChecked) tweaks + and set v = + for row in tweaks do + row.IsChecked <- v + + this.OnPropertyChanged("AllSelected") diff --git a/MacUtilGUI/ViewModels/TweakRowViewModel.fs b/MacUtilGUI/ViewModels/TweakRowViewModel.fs index 388871a..a44ba8c 100644 --- a/MacUtilGUI/ViewModels/TweakRowViewModel.fs +++ b/MacUtilGUI/ViewModels/TweakRowViewModel.fs @@ -10,6 +10,7 @@ type TweakRowViewModel(tweak: Tweak, isChecked: bool) = member _.Id = tweak.Id member _.Content = tweak.Content member _.Description = tweak.Description + member _.Category = tweak.Category member _.Tweak = tweak member this.IsChecked diff --git a/MacUtilGUI/Views/MainWindow.axaml b/MacUtilGUI/Views/MainWindow.axaml index 09e6d95..52684f3 100644 --- a/MacUtilGUI/Views/MainWindow.axaml +++ b/MacUtilGUI/Views/MainWindow.axaml @@ -49,20 +49,43 @@ Classes="macos-title" FontSize="18" Margin="0,0,0,12"/> - + - - + - - - - - + + + + + + + + + + + + + + + + + + + @@ -70,20 +93,43 @@ Classes="macos-caution" FontSize="16" Margin="0,20,0,12"/> - + - - + - - - - - + + + + + + + + + + + + + + + + + + +