-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNoteWindow.xaml.cs
More file actions
90 lines (75 loc) · 2.6 KB
/
NoteWindow.xaml.cs
File metadata and controls
90 lines (75 loc) · 2.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.ComponentModel;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;
using GlassNotes.Helpers;
using GlassNotes.Models;
using GlassNotes.Services;
namespace GlassNotes
{
public partial class NoteWindow : Window
{
private readonly NoteService _noteService;
private readonly DispatcherTimer _autoSaveTimer;
public NoteWindow(Note note, AppSettings settings)
{
InitializeComponent();
DataContext = note;
_noteService = new NoteService();
_autoSaveTimer = new DispatcherTimer { Interval = TimeSpan.FromSeconds(3) };
_autoSaveTimer.Tick += (s, e) => SaveNote();
ApplySettings(settings);
Closing += NoteWindow_Closing;
NoteTextBox.TextChanged += (s, e) =>
{
_autoSaveTimer.Stop();
_autoSaveTimer.Start();
var firstLine = NoteTextBox.Text.Split(new[] { '\r', '\n' }, StringSplitOptions.RemoveEmptyEntries).FirstOrDefault();
if (!string.IsNullOrWhiteSpace(firstLine))
{
note.Title = firstLine.Length > 30 ? firstLine.Substring(0, 30) + "..." : firstLine;
}
else
{
note.Title = "Untitled Note";
}
};
}
private void SaveNote()
{
_autoSaveTimer.Stop();
if (DataContext is Note note)
{
_noteService.SaveNote(note);
}
}
private void NoteWindow_Closing(object? sender, CancelEventArgs e)
{
SaveNote();
}
public void ApplySettings(AppSettings settings)
{
// Colors
var bgBrush = ColorHelper.GetBackgroundBrush(settings.BackgroundColor);
bgBrush.Opacity = settings.Opacity;
var textBrush = ColorHelper.GetTextBrush(settings.TextColor);
Resources["NoteBackgroundBrush"] = bgBrush;
Resources["NoteForegroundBrush"] = textBrush;
// Always on Top
Topmost = settings.AlwaysOnTop;
}
private void Window_SourceInitialized(object sender, EventArgs e)
{
ResizeHelper.Attach(this);
}
private void TitleBar_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
DragMove();
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
Close();
}
}
}