-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPageTemplatePickerWindow.xaml.cs
More file actions
160 lines (131 loc) · 6.5 KB
/
Copy pathPageTemplatePickerWindow.xaml.cs
File metadata and controls
160 lines (131 loc) · 6.5 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
using System;
using System.Windows;
using System.Windows.Interop;
using Caelum.Models;
using Caelum.Services;
using Forms = System.Windows.Forms;
namespace Caelum
{
public partial class PageTemplatePickerWindow : Window
{
private readonly bool _isNotebookCreationMode;
public PageInsertTemplate SelectedTemplate { get; private set; } = PageInsertTemplate.Blank;
public string SelectedFolderPath { get; private set; } = string.Empty;
public PageTemplatePickerWindow() : this(false, null)
{
}
public PageTemplatePickerWindow(bool notebookCreationMode, string initialFolderPath = null)
{
_isNotebookCreationMode = notebookCreationMode;
SelectedFolderPath = string.IsNullOrWhiteSpace(initialFolderPath)
? GetDefaultFolderPath()
: initialFolderPath;
InitializeComponent();
ApplyLocalization();
UpdateModeVisualState();
UpdateSelectionVisualState();
UpdateFolderPathText();
}
private void ApplyLocalization()
{
Title = _isNotebookCreationMode
? LocalizationService.Get("Home.CreateNotebookDialogTitle")
: LocalizationService.Get("Editor.InsertPageDialogTitle");
TitleTextBlock.Text = Title;
SubtitleTextBlock.Text = _isNotebookCreationMode
? LocalizationService.Get("Home.CreateNotebookDialogSubtitle")
: LocalizationService.Get("Editor.InsertPageDialogSubtitle");
BlankTitleTextBlock.Text = LocalizationService.Get("Editor.PageTemplateBlank");
BlankHintTextBlock.Text = LocalizationService.Get("Editor.PageTemplateBlankHint");
NotebookTitleTextBlock.Text = LocalizationService.Get("Editor.PageTemplateNotebook");
NotebookHintTextBlock.Text = LocalizationService.Get("Editor.PageTemplateNotebookHint");
LinedTitleTextBlock.Text = LocalizationService.Get("Editor.PageTemplateLined");
LinedHintTextBlock.Text = LocalizationService.Get("Editor.PageTemplateLinedHint");
QuadrilleTitleTextBlock.Text = LocalizationService.Get("Editor.PageTemplateQuadrille");
QuadrilleHintTextBlock.Text = LocalizationService.Get("Editor.PageTemplateQuadrilleHint");
PathLabelTextBlock.Text = LocalizationService.Get("Home.CreateNotebookPathLabel");
BrowsePathButton.ToolTip = LocalizationService.Get("Home.CreateNotebookBrowseFolder");
CreateButton.Content = LocalizationService.Get("Home.CreateNotebookAction");
CancelButton.Content = LocalizationService.Get("Common.Cancel");
}
private void UpdateModeVisualState()
{
PathSectionBorder.Visibility = _isNotebookCreationMode ? Visibility.Visible : Visibility.Collapsed;
CreateButton.Visibility = _isNotebookCreationMode ? Visibility.Visible : Visibility.Collapsed;
Height = _isNotebookCreationMode ? 660 : 560;
}
private void UpdateSelectionVisualState()
{
BlankCard.Tag = SelectedTemplate == PageInsertTemplate.Blank;
NotebookCard.Tag = SelectedTemplate == PageInsertTemplate.Notebook;
LinedCard.Tag = SelectedTemplate == PageInsertTemplate.Lined;
QuadrilleCard.Tag = SelectedTemplate == PageInsertTemplate.Quadrille;
}
private void UpdateFolderPathText()
{
SelectedFolderPathTextBlock.Text = SelectedFolderPath ?? string.Empty;
SelectedFolderPathTextBlock.ToolTip = SelectedFolderPath ?? string.Empty;
CreateButton.IsEnabled = !_isNotebookCreationMode || !string.IsNullOrWhiteSpace(SelectedFolderPath);
}
private void SelectTemplate(PageInsertTemplate template)
{
SelectedTemplate = template;
UpdateSelectionVisualState();
if (!_isNotebookCreationMode)
DialogResult = true;
}
private void BlankCard_Click(object sender, RoutedEventArgs e) => SelectTemplate(PageInsertTemplate.Blank);
private void NotebookCard_Click(object sender, RoutedEventArgs e) => SelectTemplate(PageInsertTemplate.Notebook);
private void LinedCard_Click(object sender, RoutedEventArgs e) => SelectTemplate(PageInsertTemplate.Lined);
private void QuadrilleCard_Click(object sender, RoutedEventArgs e) => SelectTemplate(PageInsertTemplate.Quadrille);
private void CancelButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
private void CloseButton_Click(object sender, RoutedEventArgs e)
{
DialogResult = false;
}
private void BrowsePathButton_Click(object sender, RoutedEventArgs e)
{
if (!_isNotebookCreationMode)
return;
using var dialog = new Forms.FolderBrowserDialog
{
Description = LocalizationService.Get("Home.CreateNotebookBrowseFolder"),
UseDescriptionForTitle = true,
ShowNewFolderButton = true,
SelectedPath = SelectedFolderPath ?? string.Empty
};
var ownerHandle = new WindowInteropHelper(this).Handle;
var result = ownerHandle != IntPtr.Zero
? dialog.ShowDialog(new Win32Window(ownerHandle))
: dialog.ShowDialog();
if (result != Forms.DialogResult.OK || string.IsNullOrWhiteSpace(dialog.SelectedPath))
return;
SelectedFolderPath = dialog.SelectedPath;
UpdateFolderPathText();
}
private void CreateButton_Click(object sender, RoutedEventArgs e)
{
if (_isNotebookCreationMode && !string.IsNullOrWhiteSpace(SelectedFolderPath))
DialogResult = true;
}
private static string GetDefaultFolderPath()
{
var documentsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
if (!string.IsNullOrWhiteSpace(documentsPath))
return documentsPath;
var desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
return string.IsNullOrWhiteSpace(desktopPath) ? string.Empty : desktopPath;
}
private sealed class Win32Window : Forms.IWin32Window
{
public Win32Window(IntPtr handle)
{
Handle = handle;
}
public IntPtr Handle { get; }
}
}
}