-
Notifications
You must be signed in to change notification settings - Fork 71
/
class_ClipboardHistory.ahk
424 lines (382 loc) · 12.8 KB
/
class_ClipboardHistory.ahk
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
;***********************************************************************************************
;
; クリップボード履歴
;
;***********************************************************************************************
;-----------------------------------------------------------------------
; クリップボード履歴の初期化
;-----------------------------------------------------------------------
InitClipboardHistory(MaxItemCount = 100) {
global ClipboardHistory := new CClipboardHistory(MaxItemCount)
}
;-----------------------------------------------------------------------
; クリップボード履歴を起動
;-----------------------------------------------------------------------
ShowClipboardHistory(x = 0, y = 0) {
global ClipboardHistory
ClipboardHistory.Update()
ClipboardHistory.Show(x, y)
}
;-----------------------------------------------------------------------
; OnClipboardChange
;-----------------------------------------------------------------------
ClipboardHistory_OnClipboardChange() {
if (IsObject(CClipboardHistory.Instance)) {
CClipboardHistory.Instance.OnClipboardChange()
}
}
;***********************************************************************************************
; ランチャー制御
;***********************************************************************************************
class CClipboardHistory
{
static Instance :=
;-----------------------------------------------------------------------
; コンストラクタ
;-----------------------------------------------------------------------
__New(MaxItemCount = 100) {
if (IsObject(CClipboardHistory.Instance)) {
this.__Delete()
}
CClipboardHistory.Instance := this
; 初期化
this.MaxItemCount := MaxItemCount
this.MaxMenuCount := 10
this.MaxMenuLen := 50
this.MaxTooltipLen := 1024
this.Items := []
this.SubMenus := ""
this.Result := 0
; メニュー生成
PumParams := {"selMethod" : "fill" ; may be "frame","fill"
;,"selBGColor" : -1 ; background color of selected item, -1 means invert, default - COLOR_MENUHILIGHT
;,"selTColor" : -1 ; text color of selected item, -1 means invert, default - COLOR_HIGHLIGHTTEXT
;,"frameWidth" : 1 ; width of select frame when selMethod = "frame"
;,"oninit" : ""
;,"onuninit" : ""
,"onselect" : "CClipboardHistory_PumHandler_"
;,"onrbutton" : ""
;,"onmbutton" : ""
,"onrun" : "CClipboardHistory_PumHandler_"
;,"onshow" : ""
,"onclose" : "CClipboardHistory_PumHandler_"
;,"pumfont" : ""
,"mnemonicCmd": "select"} ; may be "select","run"
this.Pum := new PUM(PumParams)
this.Menu := ""
}
;-----------------------------------------------------------------------
; デストラクタ
;-----------------------------------------------------------------------
__Delete() {
this.Items := ""
this.Pum.Destroy()
}
;-----------------------------------------------------------------------
; OnClipboardChange
;-----------------------------------------------------------------------
OnClipboardChange() {
Sleep, 100
ClipWait, 1
if (ErrorLevel == 0) {
this.AppendText(ClipBoard)
}
}
;-----------------------------------------------------------------------
; メニュー表示
;-----------------------------------------------------------------------
Show(x = 0, y = 0, flags = "") {
WinGet, active_id, ID, A
this.Result := 0
this.Menu.Show(x, y, flags)
this.DestroyMenu_(this.Menu)
this.SubMenus := ""
WinWaitActive, ahk_id %active_id%, , 1
WinGet, current_active_id, ID, A
if (current_active_id == active_id) {
; テキストのインデックスが返ってきた場合はペーストする
if (this.Result > 0) {
SetTextToClipboard(this.Items[this.Result, 1])
PasteFromClipboard()
; 選択されたテキストは一番新しい履歴に移動
Append := this.AppendText(this.Items[this.Result, 1], false)
if (Append) {
this.Items.Remove(this.Result)
}
}
}
}
;-----------------------------------------------------------------------
; メニュー更新
;-----------------------------------------------------------------------
Update() {
; 以前のメニューは破棄する
this.DestroyMenu_(this.Menu)
this.SubMenus := ""
this.Menu := this.CreateMenu_()
this.SubMenus := []
; 表示する数を計算
Count := this.MaxItemCount
MaxIndex := this.Items.MaxIndex()
if (Count > MaxIndex) {
Count := MaxIndex
}
; メニューに追加
Count := this.UpdateTextMenu_(this.Menu, Count)
; これ以降はサブメニューに追加
Loop {
if (Count <= 0) {
break
}
; サブメニューに追加
Index := this.NewIndex_(this.SubMenus)
this.SubMenus[Index] := this.CreateMenu_()
Count := this.UpdateTextMenu_(this.SubMenus[Index], Count)
; キャンセルを追加
this.AppendSeparator_(this.SubMenus[Index])
this.AppendCancel_(this.SubMenus[Index])
; サブメニュー登録
this.AppendSubMenu_(this.Menu, this.SubMenus[Index], "NEXT", Index + this.MaxMenuCount)
}
; キャンセルを追加
this.AppendSeparator_(this.Menu)
this.AppendCancel_(this.Menu)
}
;-----------------------------------------------------------------------
; テキスト追加
;-----------------------------------------------------------------------
AppendText(Text, Limiter = true) {
Append := false
; 格納先インデックスを取得
Index := this.NewIndex_(this.Items)
; 最新の履歴と異なるなら追加
if (Text != this.Items[Index - 1, 1]) {
if (Limiter) {
; 上限を超えているなら古いのを削除
if (Index > this.MaxItemCount) {
Loop, % Index - this.MaxItemCount {
this.Items.Remove(1)
}
Index := this.NewIndex_(this.Items)
}
}
; クリップボードの中身を追加
this.Items[Index, 1] := Text
Append := true
}
return Append
}
;-----------------------------------------------------------------------
; テキストメニュー更新
;-----------------------------------------------------------------------
UpdateTextMenu_(Byref Menu, Count) {
MenuCount := this.GetMenuCount_(Count)
Loop % MenuCount {
Index := Count - A_Index + 1
Len := StrLen(this.Items[Index, 1])
if (Len > this.MaxMenuLen) {
this.Items[Index, 2] := Len
Str := SubStr(this.Items[Index, 1], 1, this.MaxMenuLen)
this.AppendText_(Menu, Str, Index, A_Index)
}
else {
this.Items[Index, 2] := 0
this.AppendText_(Menu, this.Items[Index, 1], Index, A_Index)
}
}
Count -= MenuCount
return Count
}
;-----------------------------------------------------------------------
; メニュー数を取得
;-----------------------------------------------------------------------
GetMenuCount_(Count) {
if (Count > this.MaxMenuCount) {
MenuCount := this.MaxMenuCount
}
else {
MenuCount := Count
}
return MenuCount
}
;-----------------------------------------------------------------------
; メニューハンドラ
;-----------------------------------------------------------------------
PumHandler_(Msg, Obj) {
Index := Obj.uid
if (Msg == "onselect") {
this.PumOnSelect(Index, Obj)
}
else if (Msg == "onrun") {
this.PumOnRun(Index, Obj)
}
else if (Msg == "onclose") {
this.PumOnClose(Index, Obj)
}
else {
}
}
;-----------------------------------------------------------------------
; OnSelect
;-----------------------------------------------------------------------
PumOnSelect(Index, Obj) {
if (Index == 0) {
tooltip
}
else {
if (this.Items[Index, 2] == 0) {
tooltip
}
else {
if (this.Items[Index, 2] > this.MaxTooltipLen) {
Str := SubStr(this.Items[Index, 1], 1, this.MaxTooltipLen)
Str .= "`n`n----`n※表示文字数の上限までしか表示していません。"
}
else {
Str := this.Items[Index, 1]
}
StringReplace, Str, Str, `t, % " ", All
rect := Obj.GetRECT()
tooltip, % Str, % rect.right, % rect.top
}
}
}
;-----------------------------------------------------------------------
; OnRun
;-----------------------------------------------------------------------
PumOnRun(Index, Obj) {
this.Result := Index
}
;-----------------------------------------------------------------------
; OnClose
;-----------------------------------------------------------------------
PumOnClose(Index, Obj) {
tooltip
}
;-----------------------------------------------------------------------
; メニュー作成
;-----------------------------------------------------------------------
CreateMenu_() {
MenuParams := {"iconssize" : 16
;,"tcolor" : pumAPI.GetSysColor(7) ; default - COLOR_MENUTEXT
;,"bgcolor" : pumAPI.GetSysColor(4) ; default - COLOR_MENU
;,"nocolors" : 0
,"noicons" : 1
;,"notext" : 0
;,"maxheight" : 0
,"xmargin" : 8
,"ymargin" : 3
,"textMargin" : -20 ; this is a pixels zmount which will be added after the text to make menu look pretty
,"textoffset" : 5} ; gap between icon and item's text in pixels
Menu := this.Pum.CreateMenu(MenuParams)
return Menu
}
;-----------------------------------------------------------------------
; メニュー破棄
;-----------------------------------------------------------------------
DestroyMenu_(Byref Menu) {
if (IsObject(Menu)) {
Menu.Destroy()
}
Menu := ""
}
;-----------------------------------------------------------------------
; テキスト追加
;-----------------------------------------------------------------------
AppendText_(Byref Menu, Text, uid, Index) {
Key := this.NewAccessKey_(Index)
ItemParams := {"uid" : uid
,"name" : "&" Key A_SPACE A_SPACE Text
,"icon" : 0
;,"bold" : 0
;,"iconUseHandle" : 0
;,"break" : 0 ;0,1,2
;,"submenu" : 0
;,"tcolor" : ""
;,"bgcolor" : ""
;,"noPrefix" : 0
;,"disabled" : 0
;,"noicons" : -1 ;-1 means use parent menu's setting
;,"notext" : -1
,"issep" : 0}
Menu.Add(ItemParams)
}
;-----------------------------------------------------------------------
; キャンセル追加
;-----------------------------------------------------------------------
AppendCancel_(Byref Menu) {
CancelParams := {"uid" : 0
,"name" : "&@ キャンセル"
,"icon" : 0
;,"bold" : 0
;,"iconUseHandle" : 0
;,"break" : 0 ;0,1,2
;,"submenu" : 0
;,"tcolor" : ""
;,"bgcolor" : ""
;,"noPrefix" : 0
;,"disabled" : 0
;,"noicons" : -1 ;-1 means use parent menu's setting
;,"notext" : -1
,"issep" : 0}
Menu.Add(CancelParams)
}
;-----------------------------------------------------------------------
; サブメニュー追加
;-----------------------------------------------------------------------
AppendSubMenu_(Byref Menu, Byref SubMenu, SubMenuName, Index) {
Key := this.NewAccessKey_(Index)
CancelParams := {"uid" : 0
,"name" : "&" Key A_SPACE A_SPACE SubMenuName
,"icon" : 0
;,"bold" : 0
;,"iconUseHandle" : 0
;,"break" : 0 ;0,1,2
,"submenu" : SubMenu
;,"tcolor" : ""
;,"bgcolor" : ""
;,"noPrefix" : 0
;,"disabled" : 0
;,"noicons" : -1 ;-1 means use parent menu's setting
;,"notext" : -1
,"issep" : 0}
Menu.Add(CancelParams)
}
;-----------------------------------------------------------------------
; セパレーター追加
;-----------------------------------------------------------------------
AppendSeparator_(Byref Menu) {
Menu.Add()
}
;-----------------------------------------------------------------------
; 新規メニュー番号取得
;-----------------------------------------------------------------------
NewIndex_(Byref Items) {
Index := Items.MaxIndex()
++Index
return Index
}
;-----------------------------------------------------------------------
; 新規アクセラレータキー取得
;-----------------------------------------------------------------------
NewAccessKey_(Index) {
KeyList := "1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if (Index < 0 || StrLen(KeyList) < Index) {
Index := 1
}
Key := SubStr(KeyList, Index, 1)
return Key
}
}
;-----------------------------------------------------------------------
; メニューハンドラ
;-----------------------------------------------------------------------
CClipboardHistory_PumHandler_(Msg, Obj) {
CClipboardHistory.Instance.PumHandler_(Msg, Obj)
}
;***********************************************************************************************
; インクルード
;***********************************************************************************************
#Include <PUM/PUM_API>
#Include <PUM/PUM>
#include Clipboard.ahk