Skip to content

Commit a7795bd

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 33fb1f0 + d542965 commit a7795bd

File tree

10 files changed

+148
-84
lines changed

10 files changed

+148
-84
lines changed

src/CutCode.CrossPlatform/App.axaml.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public override void OnFrameworkInitializationCompleted()
2929
{
3030
if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
3131
{
32+
if(ThemeService.Current.Theme == ThemeType.Light) SystemColorsConfig.LightThemeColors();
33+
else SystemColorsConfig.DarkThemeColors();
3234
ThemeService.Current.ThemeChanged += (sender, args) =>
3335
{
3436
if(ThemeService.Current.Theme == ThemeType.Light) SystemColorsConfig.LightThemeColors();

src/CutCode.CrossPlatform/Helpers/Languages.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,29 @@
11
using System.Collections.Generic;
2+
using System.Linq;
3+
using CutCode.CrossPlatform.Services;
4+
using TextMateSharp.Grammars;
25

36
namespace CutCode.CrossPlatform.Helpers
47
{
58
public static class Languages
69
{
7-
8-
public static Dictionary<string, string> LanguagesDict = new Dictionary<string, string>()
10+
private static RegistryOptions _registryOptions => new(ThemeService.Current.Theme == ThemeType.Light
11+
? ThemeName.LightPlus
12+
: ThemeName.DarkPlus);
13+
public static string GetLanguagePath(Language language)
14+
{
15+
try
16+
{
17+
return LanguagesDict[language.Aliases[0]];
18+
}
19+
catch
20+
{
21+
return LanguagesDict["File"];
22+
}
23+
}
24+
private static Dictionary<string, string> LanguagesDict = new Dictionary<string, string>()
925
{
10-
{"All languages", IconPaths.File},
26+
{"File", IconPaths.File},
1127
{"Python", IconPaths.Python},
1228
{"C++", IconPaths.Cpp},
1329
{"C#", IconPaths.CSharp},

src/CutCode.CrossPlatform/Services/DatabaseService.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,8 @@ public bool EditCode(CodeModel code)
206206
if (dbCode is not null)
207207
{
208208
dbCode.Cells = code.Cells;
209+
dbCode.Title = code.Title;
210+
dbCode.Language = code.Language;
209211
_db.RunInTransaction(() =>
210212
{
211213
_db.Update(dbCode);

src/CutCode.CrossPlatform/ViewModels/AddViewModel.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using AvaloniaEdit.TextMate.Grammars;
88
using CutCode.CrossPlatform.Helpers;
99
using CutCode.CrossPlatform.Models;
10+
using CutCode.CrossPlatform.Services;
1011
using CutCode.CrossPlatform.Views;
1112
using ReactiveUI;
1213
using ReactiveUI.Fody.Helpers;
@@ -47,7 +48,7 @@ public AddViewModel(IScreen screen)
4748
IsCellEmpty = true;
4849
Cells.CollectionChanged += (sender, args) => { IsCellEmpty = Cells.Count == 0; };
4950

50-
var reg = new RegistryOptions(ThemeName.Dark);
51+
var reg = new RegistryOptions(ThemeService.Theme == ThemeType.Light ? ThemeName.LightPlus : ThemeName.DarkPlus);
5152

5253
AllLanguages = new ObservableCollection<Language>(reg.GetAvailableLanguages());
5354
SelectedLanguage2 = reg.GetLanguageByExtension(".cs");

src/CutCode.CrossPlatform/ViewModels/CodeCardViewModel.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,18 @@
77
using CutCode.CrossPlatform.Models;
88
using CutCode.CrossPlatform.Services;
99
using CutCode.CrossPlatform.Views;
10+
using DynamicData;
1011
using Newtonsoft.Json;
1112
using ReactiveUI;
1213
using ReactiveUI.Fody.Helpers;
14+
using TextMateSharp.Grammars;
1315

1416
namespace CutCode.CrossPlatform.ViewModels;
1517

1618
public class CodeCardViewModel : ViewModelBase
1719
{
1820
public CodeModel Code;
21+
private RegistryOptions _registryOptions;
1922

2023
public CodeCardViewModel(CodeModel code)
2124
{
@@ -24,6 +27,7 @@ public CodeCardViewModel(CodeModel code)
2427

2528
public void Initialise(CodeModel code)
2629
{
30+
_registryOptions = new(ThemeService.Current.Theme == ThemeType.Light ? ThemeName.LightPlus : ThemeName.DarkPlus);
2731
Navigate = ReactiveCommand.Create(Clicked);
2832
Code = code;
2933
Title = code.Title;
@@ -64,7 +68,7 @@ public void Initialise(CodeModel code)
6468
}
6569
};
6670
}
67-
71+
6872
[Reactive] public Color MainTextColor { get; set; }
6973

7074
[Reactive] public Color CardColor { get; set; }
@@ -89,6 +93,9 @@ public void Initialise(CodeModel code)
8993

9094
[Reactive] public Color IsFavouriteColor { get; set; }
9195

96+
public string LanguagePath =>
97+
Languages.GetLanguagePath(_registryOptions.GetLanguageByExtension(Language));
98+
9299
public string Language { get; set; }
93100
public long LastModificationTime { get; set; }
94101

src/CutCode.CrossPlatform/ViewModels/CodeViewModel.cs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public class CodeViewModel : PageBaseViewModel, IRoutableViewModel
1919
public Language? _language;
2020
public CodeModel Code;
2121

22+
[Reactive] public ObservableCollection<Language> AllLanguages { get; set; }
23+
24+
[Reactive] public Language SelectedLanguage { get; set; }
25+
2226
public CodeViewModel(CodeModel code)
2327
{
2428
Initialise(code);
@@ -69,9 +73,11 @@ public void Initialise(CodeModel code)
6973
Title = Code.Title;
7074

7175
RegistryOptions reg = new RegistryOptions(ThemeName.Dark);
76+
AllLanguages = new ObservableCollection<Language>(reg.GetAvailableLanguages());
7277
_language = reg.GetLanguageByExtension(code.Language);
7378
if (_language is null)
7479
UpdateToNewLanguages(code.Language, reg);
80+
SelectedLanguage = _language;
7581
Language = _language.ToString();
7682
IsEditEnabled = false;
7783

@@ -101,6 +107,12 @@ public void Initialise(CodeModel code)
101107
};
102108

103109
IsFavouritePath = code.IsFavourite ? IconPaths.StarFull : IconPaths.Star;
110+
111+
this.WhenAnyValue(x => x.SelectedLanguage).Subscribe(x =>
112+
{
113+
Language = x.ToString();
114+
_language = x;
115+
});
104116
}
105117

106118
private void UpdateToNewLanguages(string language, RegistryOptions reg)
@@ -190,7 +202,7 @@ public async void Save()
190202
{ "Code", x.Document.Text }
191203
}).ToList();
192204

193-
CodeModel editedCode = new(Title, cellsList, Language,
205+
CodeModel editedCode = new(Title, cellsList, _language.Extensions.First(),
194206
new DateTimeOffset(DateTime.UtcNow).ToUnixTimeSeconds(), Code.IsFavourite);
195207
editedCode.SetId(Code.Id);
196208

src/CutCode.CrossPlatform/Views/CodeCardView.axaml

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@
1414
<converters:StringToGeometryConverter x:Key="StringToGeometryConverter"/>
1515
</UserControl.Resources>
1616

17+
<Design.DataContext>
18+
<vm:CodeCardViewModel/>
19+
</Design.DataContext>
20+
1721
<UserControl.Styles>
1822
<Style Selector="ToggleButton:pointerover /template/ ContentPresenter">
1923
<Setter Property="Background" Value="Transparent"/>
@@ -119,9 +123,9 @@
119123
HorizontalAlignment="Left" VerticalAlignment="Center"
120124
Margin="25,15,10,15"
121125
Padding="15">
122-
<!-- <Path Stretch="UniformToFill" -->
123-
<!-- Data="{Binding Language, Converter={StaticResource StringToGeometryConverter}}" -->
124-
<!-- Fill="{Binding LanguageColor, Converter={StaticResource ColorToBrushConverter}}"/> -->
126+
<Path Stretch="UniformToFill"
127+
Data="{Binding LanguagePath, Converter={StaticResource StringToGeometryConverter}}"
128+
Fill="{Binding LanguageColor, Converter={StaticResource ColorToBrushConverter}}"/>
125129
</Viewbox>
126130
<Grid Grid.Column="1" RowDefinitions="Auto, *">
127131
<Grid Grid.Row="0" ColumnDefinitions="*, Auto, Auto">
@@ -130,7 +134,7 @@
130134
FontFamily="{StaticResource PoppinsRegular}"
131135
FontSize="17"
132136
Margin="20,20,0,8"
133-
Foreground="{Binding mainTextColor, Converter={StaticResource ColorToBrushConverter}}"/>
137+
Foreground="{Binding MainTextColor, Converter={StaticResource ColorToBrushConverter}}"/>
134138
<Viewbox Grid.Column="1"
135139
Height="22"
136140
HorizontalAlignment="Right"
@@ -140,15 +144,6 @@
140144
Fill="{Binding IsFavouriteColor, Converter={StaticResource ColorToBrushConverter}}"/>
141145
</Viewbox>
142146

143-
<Viewbox Grid.Column="2"
144-
Height="22"
145-
HorizontalAlignment="Right"
146-
Margin="0,-10,10,0">
147-
<Path Stretch="UniformToFill"
148-
Data=""
149-
Fill="{Binding LanguageColor, Converter={StaticResource ColorToBrushConverter}}"/>
150-
</Viewbox>
151-
152147
<ToggleButton Grid.Column="2" x:Name="MoreBtn"
153148
Background="Transparent"
154149
HorizontalAlignment="Right"
@@ -163,13 +158,14 @@
163158
</Viewbox>
164159
</ToggleButton>
165160
</Grid>
161+
166162
<ScrollViewer Grid.Row="1"
167163
HorizontalScrollBarVisibility="Auto" VerticalScrollBarVisibility="Auto"
168164
Margin="20,10,12,7">
169165
<Label Content="{Binding Description}"
170166
FontFamily="{StaticResource PoppinsRegular}"
171167
FontSize="14"
172-
Foreground="{Binding mainTextColor, Converter={StaticResource ColorToBrushConverter}}"/>
168+
Foreground="{Binding MainTextColor, Converter={StaticResource ColorToBrushConverter}}"/>
173169
</ScrollViewer>
174170
</Grid>
175171
</Grid>

src/CutCode.CrossPlatform/Views/CodeCellView.axaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
<cv:ColorToBrushConverter x:Key="ColorToBrushConverter"/>
1414
</UserControl.Resources>
1515

16-
16+
<Design.DataContext>
17+
<vm:CodeCellViewModel/>
18+
</Design.DataContext>
1719

1820
<UserControl.Styles>
1921
<Style Selector="Button:pointerover /template/ ContentPresenter">
@@ -116,7 +118,6 @@
116118
ShowLineNumbers="True"
117119
Height="300"
118120
Padding="10,0,10,10"
119-
IsReadOnly="{Binding !IsEditable}"
120-
/>
121+
IsReadOnly="{Binding !IsEditable}"/>
121122
</Grid>
122123
</UserControl>

src/CutCode.CrossPlatform/Views/CodeCellView.axaml.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,17 @@
1111
using AvaloniaEdit.TextMate;
1212
using AvaloniaEdit.TextMate.Grammars;
1313
using CutCode.CrossPlatform.Helpers;
14+
using CutCode.CrossPlatform.Services;
1415
using CutCode.CrossPlatform.ViewModels;
1516
using ReactiveUI;
17+
using TextMateSharp.Internal.Themes;
1618

1719
namespace CutCode.CrossPlatform.Views
1820
{
1921
public class CodeCellView : ReactiveUserControl<CodeCellViewModel>
2022
{
2123
private TextEditor TextEditor => this.FindControl<TextEditor>(nameof(TextEditor));
22-
private readonly TextMate.Installation _textMateInstallation;
24+
private TextMate.Installation _textMateInstallation;
2325
private RegistryOptions _registryOptions;
2426

2527
public CodeCellView()
@@ -33,19 +35,30 @@ public CodeCellView()
3335

3436
TextEditor.TextArea.IndentationStrategy = new CSharpIndentationStrategy(TextEditor.Options);
3537

36-
_registryOptions = new RegistryOptions(ThemeName.Dark);
38+
_registryOptions = new RegistryOptions(ThemeService.Current.Theme == ThemeType.Light ? ThemeName.LightPlus : ThemeName.DarkPlus);
39+
ThemeService.Current.ThemeChanged += (sender, args) =>
40+
{
41+
_registryOptions = new RegistryOptions(ThemeService.Current.Theme == ThemeType.Light ? ThemeName.LightPlus : ThemeName.DarkPlus);
42+
_textMateInstallation = TextEditor.InstallTextMate(_registryOptions);
43+
44+
var scopeName = _registryOptions.GetScopeByLanguageId(_currentLanguage!.Id);
45+
_textMateInstallation.SetGrammar(scopeName);
46+
};
47+
3748
_textMateInstallation = TextEditor.InstallTextMate(_registryOptions);
3849

39-
Language csharpLanguage = _registryOptions.GetLanguageByExtension(".cs");
50+
_currentLanguage = _registryOptions.GetLanguageByExtension(".cs");
4051

41-
string scopeName = _registryOptions.GetScopeByLanguageId(csharpLanguage.Id);
52+
var scopeName = _registryOptions.GetScopeByLanguageId(_currentLanguage.Id);
4253
_textMateInstallation.SetGrammar(scopeName);
4354
GlobalEvents.OnLanguageSet += GlobalEventsOnOnLanguageSet;
4455
GlobalEvents.ViewRegistered(this);
4556
}
4657

58+
private Language _currentLanguage;
4759
private void GlobalEventsOnOnLanguageSet(object? sender, Language e)
4860
{
61+
_currentLanguage = e;
4962
string scopeName = _registryOptions.GetScopeByLanguageId(e.Id);
5063
_textMateInstallation.SetGrammar(scopeName);
5164
}

0 commit comments

Comments
 (0)