-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDesktopShortcuts.Form.pas
More file actions
150 lines (126 loc) · 3.82 KB
/
Copy pathDesktopShortcuts.Form.pas
File metadata and controls
150 lines (126 loc) · 3.82 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
unit DesktopShortcuts.Form;
interface
uses
System.Classes,
System.SysUtils,
Winapi.Messages,
Winapi.Windows,
Vcl.Controls,
Vcl.Dialogs,
Vcl.ExtCtrls,
Vcl.Forms,
Vcl.Grids,
Vcl.Menus,
Vcl.StdCtrls,
DesktopShortcuts.Settings;
type
TDesktopShortcutsForm = class(TForm)
btnCancelar: TButton;
btnGravar: TButton;
lbInstrucao: TLabel;
pnBotoes: TPanel;
strGridAtalhos: TStringGrid;
procedure btnGravarClick(ASender: TObject);
procedure FormShortCut(var AMessage: TWMKey; var AHandled: Boolean);
procedure strGridAtalhosKeyDown(ASender: TObject; var AKey: Word;
AShift: TShiftState);
private
FItems: TDesktopShortcutItems;
function ApplyShortcut(AKey: Word; AShift: TShiftState): Boolean;
procedure LoadConfiguration(ADesktopNames: TStrings);
function ValidateConfiguration: Boolean;
public
class function Execute(ADesktopNames: TStrings): Boolean; static;
end;
implementation
{$R *.dfm}
function TDesktopShortcutsForm.ApplyShortcut(AKey: Word;
AShift: TShiftState): Boolean;
begin
Result := False;
if not strGridAtalhos.Focused or (strGridAtalhos.Row <= 0) or
(strGridAtalhos.Row > Length(FItems)) then
Exit;
if AKey in [VK_SHIFT, VK_CONTROL, VK_MENU, VK_TAB, VK_RETURN, VK_ESCAPE] then
Exit;
var LIndex := Pred(strGridAtalhos.Row);
if AKey in [VK_DELETE, VK_BACK] then
FItems[LIndex].Shortcut := 0
else
begin
var LModifiers := AShift * [ssShift, ssCtrl, ssAlt];
if (LModifiers = []) and ((AKey < VK_F1) or (AKey > VK_F24)) then
Exit;
FItems[LIndex].Shortcut := Vcl.Menus.ShortCut(AKey, LModifiers);
end;
strGridAtalhos.Cells[1, strGridAtalhos.Row] := ShortCutToText(FItems[LIndex].Shortcut);
Result := True;
end;
procedure TDesktopShortcutsForm.btnGravarClick(ASender: TObject);
begin
if not Self.ValidateConfiguration then
Exit;
TDesktopShortcutSettings.Save(FItems);
ModalResult := mrOk;
end;
procedure TDesktopShortcutsForm.FormShortCut(var AMessage: TWMKey;
var AHandled: Boolean);
begin
AHandled := Self.ApplyShortcut(AMessage.CharCode, KeyboardStateToShiftState);
end;
class function TDesktopShortcutsForm.Execute(ADesktopNames: TStrings): Boolean;
begin
var LForm := TDesktopShortcutsForm.Create(nil);
try
LForm.LoadConfiguration(ADesktopNames);
Result := LForm.ShowModal = mrOk;
finally
LForm.Free;
end;
end;
procedure TDesktopShortcutsForm.LoadConfiguration(ADesktopNames: TStrings);
begin
TDesktopShortcutSettings.Load(FItems);
TDesktopShortcutSettings.Synchronize(ADesktopNames, FItems);
strGridAtalhos.Cells[0, 0] := 'Desktop';
strGridAtalhos.Cells[1, 0] := 'Atalho';
strGridAtalhos.ColWidths[0] := 370;
strGridAtalhos.ColWidths[1] := 180;
if Length(FItems) > 0 then
strGridAtalhos.RowCount := Succ(Length(FItems))
else
strGridAtalhos.RowCount := 2;
for var i := 0 to High(FItems) do
begin
var LRow := Succ(i);
strGridAtalhos.Cells[0, LRow] := FItems[i].DesktopName;
strGridAtalhos.Cells[1, LRow] := ShortCutToText(FItems[i].Shortcut);
end;
end;
procedure TDesktopShortcutsForm.strGridAtalhosKeyDown(ASender: TObject;
var AKey: Word; AShift: TShiftState);
begin
if Self.ApplyShortcut(AKey, AShift) then
AKey := 0;
end;
function TDesktopShortcutsForm.ValidateConfiguration: Boolean;
begin
Result := False;
for var i := 0 to High(FItems) do
begin
if FItems[i].Shortcut = 0 then
Continue;
for var j := Succ(i) to High(FItems) do
if FItems[i].Shortcut = FItems[j].Shortcut then
begin
MessageDlg(Format('O atalho %s esta associado aos Desktops "%s" e "%s".',
[ShortCutToText(FItems[i].Shortcut), FItems[i].DesktopName,
FItems[j].DesktopName]), mtWarning, [mbOK], 0);
strGridAtalhos.Row := Succ(j);
strGridAtalhos.SetFocus;
Exit;
end;
end;
Result := True;
end;
end.