-
Notifications
You must be signed in to change notification settings - Fork 21
/
edraw-property-editor.el
2216 lines (1915 loc) · 85.9 KB
/
edraw-property-editor.el
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; edraw-property-editor.el --- Property Editor -*- lexical-binding: t; -*-
;; Copyright (C) 2022 AKIYAMA Kouhei
;; Author: AKIYAMA Kouhei <misohena@gmail.com>
;; Keywords: Graphics,Drawing,SVG
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'widget)
(require 'wid-edit)
(require 'edraw-util)
(require 'edraw-math)
(require 'edraw-color-picker)
(require 'edraw-editor-util)
(require 'edraw-dom-svg)
(declare-function edraw-node-position "edraw")
(declare-function edraw-node-siblings-count "edraw")
(declare-function edraw-preset-save "edraw" (ui-state type name data))
(declare-function edraw-preset-enum-names "edraw" (ui-state type &optional pred))
(declare-function edraw-preset-load "edraw" (ui-state type name))
(declare-function edraw-preset-delete "edraw" (ui-state type name))
(declare-function edraw-preset-rename "edraw" (ui-state type old-name new-name))
(declare-function edraw-preset-clear "edraw" (ui-state type))
(declare-function edraw-preset-apply "edraw")
;;;; Property Editor Target
;;;;; Base Class
(defclass edraw-properties-holder ()
()
:abstract t)
;;;;; Object Name
(cl-defgeneric edraw-name (object)
"Return the name of OBJECT.")
;;;;; Get/Set Properties
(cl-defgeneric edraw-get-property-info-list (object)
"Return information on all properties that OBJECT can have.
Return a list of property information.
Each list element is an `edraw-svg-prop-info' object.")
(cl-defgeneric edraw-get-property-info (object prop-name)
"Return information on the property with the name specified by PROP-NAME.
Return an `edraw-svg-prop-info' object that holds property information.
The default implementation calls `edraw-get-property-info-list'
and searches it for a property named PROP-NAME."
(edraw-svg-prop-info-info-list-find
(edraw-get-property-info-list object)
prop-name))
(cl-defgeneric edraw-can-have-property-p (object prop-name)
"Return t if OBJECT can have a property named PROP-NAME.
Return nil if the property named PROP-NAME is not valid for OBJECT."
(when (edraw-get-property-info object prop-name)
t))
(cl-defgeneric edraw-get-property (object prop-name)
"Return the value of the property named PROP-NAME of OBJECT.
PROP-NAME is a symbol that identifies a property.")
(cl-defgeneric edraw-set-property (object prop-name value)
"Set the value of the property named PROP-NAME of OBJECT to VALUE.
PROP-NAME is a symbol that identifies a property.
Generally has the same effect as calling `edraw-set-properties'
on an alist with one element.
Use `edraw-set-properties' to change multiple properties at the
same time. It behaves more efficiently and consistently."
(edraw-set-properties object (list (cons prop-name value))))
(cl-defgeneric edraw-set-properties (object prop-alist)
"Set the property values specified in PROP-ALIST to OBJECT.
PROP-ALIST is an alist of (PROP-NAME . VALUE).
PROP-NAME is a symbol that identifies a property.")
(cl-defgeneric edraw-get-all-properties (object)
"Return an alist of all property values that OBJECT has.
For all properties that can be obtained with
`edraw-get-property-info-list', returns an alist of property
values (PROP-NAME . VALUE) obtained by calling
`edraw-get-property'."
(cl-loop for prop-info in (edraw-get-property-info-list object)
collect (let ((prop-name (edraw-svg-prop-info-name prop-info)))
(cons prop-name
(edraw-get-property object prop-name)))))
;;;;; Undo Support
;; @todo It appears to undo changes to OBJECT, but it actually undoes
;; the editor associated with OBJECT. It is difficult to understand
;; because it does not necessarily mean that only changes to OBJECT
;; are undone.
(cl-defgeneric edraw-undo-block-begin (object))
(cl-defgeneric edraw-undo-block-end (object backup))
(cl-defgeneric edraw-undo-all (object))
(cl-defgeneric edraw-last-undo-data (object))
(cl-defgeneric edraw-undo (object))
;;;;; Hook
(cl-defgeneric edraw-add-change-hook (object function &rest args)
"Add a FUNCTION to be called when OBJECT changes.
The arguments passed to the FUNCTION are the expansion of ARGS,
OBJECT, a symbol or other structure representing the type of
change, and additional information (hint) about the change.
(FUNCTION ARGS... OBJECT TYPE HINT)
See `edraw-hook-add'.")
(cl-defgeneric edraw-remove-change-hook (object function &rest args)
"Remove the FUNCTION that is called when OBJECT changes.
Remove the function added with `edraw-add-change-hook'.
FUNCTION and each element of ARGS must be `eq' with those
specified when added.
See `edraw-hook-remove'.")
;;;;; Selection
(cl-defgeneric edraw-select (object)
"Select OBJECT.
The detailed meaning depends on the type of OBJECT.")
;;;;; Settings
(cl-defgeneric edraw-set-all-properties-as-default (object)
"Set all properties of OBJECT to default values for objects of
the same type.")
;;;;; Preset
(cl-defgeneric edraw-preset-type (_presettable)
"Return the preset data type of PRESETTABLE object."
nil)
(cl-defgeneric edraw-preset-subtype (_presettable)
"Return the preset data subtype of PRESETTABLE object."
nil)
(cl-defgeneric edraw-preset-tool-type (_presettable)
nil)
(cl-defgeneric edraw-preset-properties (_presettable)
"Generate preset data properties from PRESETTABLE object and return it."
nil)
(cl-defgeneric edraw-preset-data (presettable)
"Generate preset data from PRESETTABLE object and return it.
To extract more detailed information from the returned preset
data, the following functions may be available:
- `edraw-preset-data-subtype'
- `edraw-preset-data-properties'"
(let ((subtype (edraw-preset-subtype presettable))
(properties (edraw-preset-properties presettable)))
(when properties
(nconc
(when subtype
(list (cons 'subtype subtype)))
(list (cons 'properties properties))))))
(defun edraw-preset-data-subtype (data)
"Get subtype from DATA created by `edraw-preset-data' function."
(alist-get 'subtype data))
(defun edraw-preset-data-properties (data)
"Get properties from DATA created by `edraw-preset-data' function."
(alist-get 'properties data))
(defun edraw-preset-name-special (category subtype)
(list 'special category subtype))
(defun edraw-preset-name-special-p (name)
(and (listp name) (eq (car name) 'special)))
(defun edraw-preset-name-special-category (name) (nth 1 name))
(defun edraw-preset-name-special-subtype (name) (nth 2 name))
(defun edraw-preset-name-special-caption (name)
(pcase (edraw-preset-name-special-category name)
('initial-default-shape
(format (edraw-msg "(Initial %s Shape Default)")
;; shape-type to capitalized string
(edraw-msg (capitalize (symbol-name (edraw-preset-name-special-subtype name))))))
('initial-default-shape-for-tool
(format (edraw-msg "(Initial %s Tool Default)")
;; shape-type to capitalized stringe
(let ((tool-class (edraw-preset-name-special-subtype name)))
(if (and (class-p tool-class)
(child-of-class-p tool-class 'edraw-editor-tool))
;; Capitalized and Localized name
(edraw-name tool-class)
(edraw-msg (capitalize (symbol-name tool-class)))))))
('initial-default-marker
(format (edraw-msg "(Initial %s Marker Default)")
;; marker-type to capitalized string
(edraw-msg (capitalize (symbol-name (edraw-preset-name-special-subtype name))))))
;; Unknown
(category
(format "%s" category))))
(defun edraw-preset-name-as-string (name)
(cond
((edraw-preset-name-special-p name)
(edraw-preset-name-special-caption name))
((stringp name) name)
(t (format "%s" name))))
(defun edraw-preset-name-less-p (n1 n2)
(if (edraw-preset-name-special-p n1)
(if (edraw-preset-name-special-p n2)
(string< (format "%s %s"
(edraw-preset-name-special-category n1)
(edraw-preset-name-special-subtype n1))
(format "%s %s"
(edraw-preset-name-special-category n2)
(edraw-preset-name-special-subtype n2)))
t)
(if (edraw-preset-name-special-p n2)
nil
(string< n1 n2))))
;;;;; Type Predicate
(cl-defgeneric edraw-property-editor-shape-p (_object)
"Return non-nil if OBJECT is a derived class of edraw-shape.
Used by the property editor to determine the type of object."
nil)
;;;;; Extra UI
(cl-defgeneric edraw-property-editor-actions (_object)
nil)
(cl-defgeneric edraw-update-from-property-editor (_object)
nil)
;;;;; Implementation with alist
(defclass edraw-alist-properties-holder (edraw-properties-holder)
((prop-info-list :initarg :prop-info-list)
(alist-head :initarg :alist-head) ;;(??? (prop . value) ...)
(editor :initarg :editor)
(name :initarg :name)
(change-hook :initform (edraw-hook-make))
(preset-type :initarg :preset-type :initform nil)
(preset-subtype :initarg :preset-subtype :initform nil)))
(cl-defmethod edraw-set-alist-head ((holder edraw-alist-properties-holder)
alist-head)
(oset holder alist-head alist-head))
(cl-defmethod edraw-set-prop-info-list ((holder edraw-alist-properties-holder)
prop-info-list)
(oset holder prop-info-list prop-info-list))
(cl-defmethod edraw-get-editor ((holder edraw-alist-properties-holder))
(oref holder editor))
(cl-defmethod edraw-name ((holder edraw-alist-properties-holder))
(oref holder name))
(cl-defmethod edraw-undo-block-begin ((_holder edraw-alist-properties-holder)))
(cl-defmethod edraw-undo-block-end ((_holder edraw-alist-properties-holder) _backup))
(cl-defmethod edraw-undo-all ((_holder edraw-alist-properties-holder)))
(cl-defmethod edraw-last-undo-data ((_holder edraw-alist-properties-holder)))
(cl-defmethod edraw-undo ((_holder edraw-alist-properties-holder)))
(cl-defmethod edraw-get-property-info-list ((holder edraw-alist-properties-holder))
(oref holder prop-info-list))
(cl-defmethod edraw-get-property ((holder edraw-alist-properties-holder) prop-name)
(alist-get prop-name (cdr (oref holder alist-head))))
(cl-defmethod edraw-set-properties ((holder edraw-alist-properties-holder) prop-list)
(let (changed)
(dolist (prop prop-list)
(let* ((prop-name (car prop))
(new-value (cdr prop))
(prev (let ((prev (oref holder alist-head)))
(while (and (cdr prev)
(not (eq (caadr prev) prop-name)))
(setq prev (cdr prev)))
prev))
(old-value (cdadr prev)))
(unless (equal new-value old-value)
(cond
;; Remove
((null new-value)
(setcdr prev (cddr prev)))
;; Add
((null (cdr prev))
(setcdr prev (cons (cons prop-name new-value) nil)))
;; Modify
(t
(setcdr (cadr prev) new-value)))
(setq changed t))))
(when changed
(edraw-hook-call (oref holder change-hook) holder 'alist-properties nil))
changed))
(cl-defmethod edraw-add-change-hook ((holder edraw-alist-properties-holder)
function &rest args)
(apply 'edraw-hook-add (oref holder change-hook) function args))
(cl-defmethod edraw-remove-change-hook ((holder edraw-alist-properties-holder)
function &rest args)
(apply 'edraw-hook-remove (oref holder change-hook) function args))
(cl-defmethod edraw-preset-type ((holder edraw-alist-properties-holder))
(oref holder preset-type))
(cl-defmethod edraw-preset-subtype ((holder edraw-alist-properties-holder))
(oref holder preset-subtype))
;;;; Property Editor Variables
(defgroup edraw-faces nil
"Faces in Edraw."
:tag "Edraw Faces"
:group 'edraw)
(defconst edraw-property-editor-push-button-prefix " ")
(defconst edraw-property-editor-push-button-suffix " ")
(defface edraw-widget-button
'((((type x w32 ns) (class color))
:box (:line-width 2 :style released-button)
:background "lightgrey" :foreground "black"))
"Widget button face."
:group 'edraw-faces)
(defface edraw-widget-button-mouse
'((((type x w32 ns) (class color))
:box (:line-width 2 :style released-button)
:background "grey90" :foreground "black")
(t :inverse-video t))
"Widget button mouse face."
:group 'edraw-faces)
(defface edraw-widget-button-pressed
'((((type x w32 ns) (class color))
:box (:line-width 2 :style pressed-button)
:background "lightgrey" :foreground "black")
(t :inverse-video t))
"Widget button pressed face."
:group 'edraw-faces)
(defgroup edraw-property-editor nil
"Edit object properties in list format."
:tag "Edraw Property Editor"
:prefix "edraw-property-editor-"
:group 'edraw-editor)
(defcustom edraw-property-editor-apply-immediately t
"non-nil means that the entered value will be reflected immediately."
:group 'edraw-property-editor
:type 'boolean)
(defcustom edraw-property-editor-tracking-selected-shape t
"non-nil means to switch the editing target of the property
editor when the selected shape changes."
:group 'edraw-property-editor
:type 'boolean)
(defun edraw-property-editor-tracking-selected-shape-p ()
edraw-property-editor-tracking-selected-shape)
(defcustom edraw-property-editor-close-on-remove-shape nil
"non-nil means close the property editor when the editing target is removed."
:group 'edraw-property-editor
:type 'boolean)
(defvar edraw-property-editor-buffer-name "*Easy Draw Property Editor*")
(defvar edraw-property-editor-push-button-map
(let ((km (make-sparse-keymap)))
(define-key km [drag-mouse-1] 'ignore)
(define-key km [double-down-mouse-1] 'edraw-property-editor-widget-button-click)
(define-key km [triple-down-mouse-1] 'edraw-property-editor-widget-button-click)
km))
(defun edraw-property-editor-define-field-map-keys (km)
(define-key km (kbd "C-c C-c") 'edraw-property-editor--apply)
(define-key km (kbd "C-c C-k") 'edraw-property-editor--close)
(define-key km [drag-mouse-1] 'ignore)
(define-key km [double-mouse-1] 'ignore)
(define-key km [triple-mouse-1] 'ignore)
(define-key km [down-mouse-2] 'ignore)
(define-key km [mouse-2] 'edraw-property-editor--close)
(define-key km [mouse-3] 'edraw-property-editor--menu)
(define-key km [C-wheel-down] 'edraw-property-editor-field-wheel-decrease)
(define-key km [C-wheel-up] 'edraw-property-editor-field-wheel-increase))
(defvar edraw-property-editor-field-map
(let ((km (make-sparse-keymap)))
(set-keymap-parent km widget-field-keymap)
(edraw-property-editor-define-field-map-keys km)
km))
(defvar edraw-property-editor-text-map
(let ((km (make-sparse-keymap)))
(set-keymap-parent km widget-text-keymap) ;;For multiline
(edraw-property-editor-define-field-map-keys km)
km))
(defvar edraw-property-editor-mode-map
(let ((km (make-sparse-keymap)))
(set-keymap-parent km widget-keymap)
(define-key km (kbd "C-c C-c") 'edraw-property-editor--apply)
(define-key km (kbd "C-c C-k") 'edraw-property-editor--close)
(define-key km [drag-mouse-1] 'ignore)
(define-key km [double-mouse-1] 'ignore)
(define-key km [double-down-mouse-1] 'ignore)
(define-key km [triple-mouse-1] 'ignore)
(define-key km [down-mouse-2] 'ignore)
(define-key km [mouse-2] 'edraw-property-editor--close)
(define-key km [mouse-3] 'edraw-property-editor--menu)
km))
;;;; Major Mode
(define-derived-mode edraw-property-editor-mode nil "Eprops"
;; Disable context-menu-mode
(setq-local minor-mode-overriding-map-alist
'((context-menu-mode . nil)))
;; Disable indentation
(setq-local indent-line-function
#'edraw-property-editor-mode-indent))
(defun edraw-property-editor-mode-indent ()
nil)
;;;; Property Editor
(defclass edraw-property-editor ()
((buffer :initarg :buffer)
(display :initarg :display)
(target :initform nil)
(widgets)
(update-timer :initform nil)
(last-edit-undo-data :initform nil)
(last-edit-prop-name :initform nil)
(options :initarg :options)
(ui-state :initarg :ui-state)))
(defvar-local edraw-property-editor--pedit nil)
(defun edraw-property-editor-buffer ()
(get-buffer edraw-property-editor-buffer-name))
(defun edraw-property-editor-close ()
(when-let ((buffer (edraw-property-editor-buffer)))
(with-current-buffer buffer
(when edraw-property-editor--pedit
(edraw-close edraw-property-editor--pedit)))))
(defun edraw-property-editor-open (target &optional options)
(let* ((buffer (get-buffer-create edraw-property-editor-buffer-name))
(ui-state (or (alist-get 'ui-state options)
(edraw-ui-state-object-default)))
;; Get property editor object
(pedit (if-let ((pedit (with-current-buffer buffer
edraw-property-editor--pedit)))
(progn
;; Update options
(oset pedit options options)
(oset pedit ui-state ui-state)
pedit)
;; New property editor object
(edraw-property-editor-create-object buffer options ui-state))))
(unless (eq target (oref pedit target))
(with-current-buffer buffer
;; Release current target
(edraw-unobserve-target pedit)
;; Activating major mode does following:
;; - Call (kill-all-local-variables)
;; - Call (use-local-map edraw-property-editor-mode-map)
;;@todo need every time?
(edraw-property-editor-mode)
(setq-local edraw-property-editor--pedit pedit)
(edraw-observe-target pedit target)))
;; Open window or frame
(edraw-display-buffer pedit)))
(defun edraw-property-editor-create-object (buffer options ui-state)
(edraw-property-editor
:buffer buffer
:display (edraw-buffer-display
:buffer buffer
:frame-parameters-default
(append
'((title . "Edraw Property Editor"))
edraw-buffer-display-frame-parameters-default)
:frame-parameters-last
(edraw-ui-state-get ui-state 'property-editor 'frame-parameters-last)
:frame-mode
(edraw-ui-state-get ui-state 'property-editor 'frame-mode)
:frame-mode-line-p
(edraw-ui-state-get ui-state 'property-editor 'frame-mode-line-p)
:frame-child-p
(edraw-ui-state-get ui-state 'property-editor 'frame-child-p)
:save-function
(lambda (_obj key value)
(edraw-ui-state-set ui-state 'property-editor key value)
(edraw-ui-state-save ui-state)))
:options options
:ui-state ui-state))
(defun edraw-property-editor-target-shape-p (target)
(and target
;;(cl-typep target 'edraw-shape) ;;warning
(edraw-property-editor-shape-p target)))
(cl-defmethod edraw-observe-target ((pedit edraw-property-editor) new-target)
(with-slots (target) pedit
(edraw-unobserve-target pedit)
(setq target new-target)
(edraw-set-last-edit pedit nil nil)
(edraw-update-buffer pedit)
(edraw-initialize-hooks pedit)))
(cl-defmethod edraw-update-buffer ((pedit edraw-property-editor))
(with-slots (target widgets) pedit
(setq widgets nil)
(let ((inhibit-read-only t))
(erase-buffer))
(remove-overlays)
(setq-local widget-push-button-prefix edraw-property-editor-push-button-prefix)
(setq-local widget-push-button-suffix edraw-property-editor-push-button-suffix)
(setq-local widget-link-prefix "")
(setq-local widget-link-suffix "")
(setq-local widget-button-face 'edraw-widget-button)
(setq-local widget-button-pressed-face 'edraw-widget-button-pressed)
(setq-local widget-mouse-face 'edraw-widget-button-mouse)
;; Title
(unless target
(widget-insert (edraw-msg "No target object") "\n\n"))
(when target
(widget-insert (format (edraw-msg "Properties of %s")
(or (edraw-name target) "")))
;; Prev / Next
(when (edraw-property-editor-target-shape-p target)
(widget-insert " ")
(let* ((beg (point))
(shape-index (edraw-property-editor--shape-index))
(num-shapes (edraw-property-editor--num-shapes))
(num-shapes-digits (if (< num-shapes 10)
1
(1+ (floor (log num-shapes 10)))))
prev next)
(setq prev
(widget-create 'push-button
:notify 'edraw-property-editor--prev
:keymap edraw-property-editor-push-button-map
(edraw-msg "Prev")))
(when (<= shape-index 0)
(widget-apply prev :deactivate))
(widget-insert " "
(format (format "%%%dd/%%%dd"
num-shapes-digits
num-shapes-digits)
(1+ shape-index) num-shapes)
" ")
(setq next
(widget-create 'push-button
:notify 'edraw-property-editor--next
:keymap edraw-property-editor-push-button-map
(edraw-msg "Next")))
(when (>= shape-index (1- num-shapes))
(widget-apply next :deactivate))
;; Align to right
(let ((ui-width (+ (string-width (buffer-substring beg (point)))
4)))
(put-text-property (1- beg) beg 'display
`(space :align-to (- right ,ui-width))))))
(widget-insert "\n"))
;; Actions
(when target
(edraw-property-editor-insert-action-bar target))
;; Properties
(when target
(setq widgets (edraw-insert-property-widgets pedit target 0 0)))
;; Bottom
(edraw-property-editor--update-buffer--button-line target)
(widget-insert "\n")
(widget-setup)
(widget-forward 1) ;;to first field
;; (message
;; (format (substitute-command-keys
;; "\\[edraw-property-editor--apply]: %s \\[edraw-property-editor--close]: %s")
;; (edraw-msg "Apply")
;; (edraw-msg "Close")))
))
(defun edraw-property-editor-insert-action-bar (target)
(when-let ((widgets (edraw-property-editor-actions target)))
(widget-insert " ")
(dolist (widget-args widgets)
(widget-insert " ")
(apply #'widget-create widget-args))
(widget-insert "\n")))
(cl-defmethod edraw-insert-property-widgets ((pedit edraw-property-editor)
target
margin-left
min-name-width)
(let* ((prop-info-list (edraw-get-property-info-list target))
(max-name-width (when prop-info-list
(apply #'max
(mapcar
(lambda (prop-info)
(string-width
(edraw-property-editor-property-display-name
(edraw-svg-prop-info-name prop-info))))
prop-info-list))))
widgets)
(dolist (prop-info prop-info-list)
(unless (edraw-svg-prop-info-internal-p prop-info)
(push (edraw-create-prop-widget pedit target prop-info
margin-left
(max min-name-width max-name-width))
widgets)))
;; Return widgets
(nreverse widgets)))
(defun edraw-property-editor--update-buffer--button-line (target)
(widget-insert (make-string 2 ? ))
(when target
(when (edraw-property-editor-target-shape-p target)
(widget-create 'push-button
:notify 'edraw-property-editor--set-as-default
:keymap edraw-property-editor-push-button-map
(edraw-msg "Set as default"))
(widget-insert " "))
(when (edraw-preset-type target)
(widget-create 'push-button
:notify 'edraw-property-editor--preset-menu
:keymap edraw-property-editor-push-button-map
(edraw-msg "Preset"))
(widget-insert " "))
(unless edraw-property-editor-apply-immediately
(widget-create 'push-button :notify 'edraw-property-editor--apply
(edraw-msg "Apply"))
(widget-insert " ")))
(widget-create 'push-button :notify 'edraw-property-editor--close
(edraw-msg "Close"))
(widget-insert " ")
(widget-create 'push-button :notify 'edraw-property-editor--menu
(edraw-msg "Menu"))
(widget-insert "\n"))
;;;;; Window/Frame
(cl-defmethod edraw-display-buffer ((pedit edraw-property-editor))
(with-slots (display) pedit
(edraw-display-buffer display)))
;;;;; Prop Widget
(cl-defmethod edraw-create-prop-widget ((pedit edraw-property-editor)
target prop-info
margin-left
name-column-width)
(let* ((notify (edraw-create-prop-widget-updator
pedit target prop-info)))
(edraw-property-editor-prop-widget-create-widget
target prop-info margin-left name-column-width notify pedit)))
(defclass edraw-property-editor-prop-widget ()
((widget :initarg :widget)
(target :initarg :target)
(prop-info :initarg :prop-info)))
(cl-defmethod edraw-get-target ((pw edraw-property-editor-prop-widget))
(oref pw target))
(cl-defmethod edraw-widget-delete ((pw edraw-property-editor-prop-widget))
(widget-delete (oref pw widget)))
;;;;;; Prop Widget - Update Property From Widget
(defvar edraw-property-editor-prop-widget--notification-suppressed nil)
(defun edraw-property-editor-prop-widget-value-set-without-notify
(widget new-w-value)
"Same as widget-value-set, but suppresses widget change notifications."
(let ((edraw-property-editor-prop-widget--notification-suppressed t))
(save-excursion ;; `widget-field-value-set' will change the current point!
(widget-value-set widget new-w-value))))
(defun edraw-property-editor-prop-widget-value-set (widget new-w-value)
"Same as widget-value-set, but suppresses property update
once. widget-value-set updates the same property four times."
;; Change widget text without notification
(edraw-property-editor-prop-widget-value-set-without-notify
widget new-w-value)
;; Notify only once
(widget-apply widget :notify widget nil));;event=nil
(cl-defmethod edraw-last-edit-undo-data ((pedit edraw-property-editor))
(oref pedit last-edit-undo-data))
(cl-defmethod edraw-last-edit-prop-name ((pedit edraw-property-editor))
(oref pedit last-edit-prop-name))
(cl-defmethod edraw-set-last-edit ((pedit edraw-property-editor)
undo-data prop-name)
(oset pedit last-edit-undo-data undo-data)
(oset pedit last-edit-prop-name prop-name))
(cl-defmethod edraw-create-prop-widget-updator ((pedit edraw-property-editor)
target
prop-info)
(let ((prop-name (edraw-svg-prop-info-name prop-info)))
(lambda (widget _changed-widget &optional event)
;;(message "on widget changed %s event=%s" (edraw-svg-prop-info-name prop-info) event)
;; Called 4 times per widget-value-set call.
;; 1. delete chars (event=(before-change BEG END))
;; 2. delete chars (event=(after-change BEG END))
;; 3. insert chars (event=(before-change BEG END))
;; 4. insert chars (event=(after-change BEG END))
(when (and
(not (eq (car-safe event) 'before-change)) ;;Ignore before-change
(not edraw-property-editor-prop-widget--notification-suppressed))
(if (eq target (oref pedit target))
;; For top-level target
(when edraw-property-editor-apply-immediately
(progn
;;(message "Set property toplevel value=%s" (widget-value widget))
(edraw-set-target-property-value
pedit prop-name
(edraw-property-editor-widget-value-to-prop-value
(widget-value widget) prop-info))))
;; For sub-level targets
;;(message "Set property sublevel value=%s" (widget-value widget))
;; NOTE: For sub-property, even if
;; edraw-property-editor-apply-immediately is nil, it will
;; be reflected immediately.
;; Because the value must already be reflected in the widget
;; when the Apply button is pressed.
(edraw-set-property
target prop-name
(edraw-property-editor-widget-value-to-prop-value
(widget-value widget) prop-info)))))))
(cl-defmethod edraw-set-target-property-value ((pedit edraw-property-editor)
prop-name prop-value)
(when-let ((target (oref pedit target)))
(let ((undo-before-change (edraw-last-undo-data target)))
;; Consecutive change to the same target and same property?
(when (and (eq undo-before-change (edraw-last-edit-undo-data pedit))
(eq prop-name (edraw-last-edit-prop-name pedit)))
(edraw-undo target)
(setq undo-before-change (edraw-last-undo-data target)))
;; Change property
;;(message "Set property %s %s" prop-name prop-value)
(edraw-set-property
target
prop-name
prop-value)
;; Record last change
(let ((undo-after-change (edraw-last-undo-data target)))
(if (eq undo-before-change undo-after-change)
;; No undo data generated (Probably same values)
(edraw-set-last-edit pedit nil nil)
;; New undo data generated
(edraw-set-last-edit pedit undo-after-change prop-name))))))
;;;;;; Prop Widget - Create Widget
(defun edraw-property-editor-prop-widget-create-widget (target
prop-info
margin-left
name-column-width
notify
pedit)
(let* ((prop-name (edraw-svg-prop-info-name prop-info))
(prop-value (edraw-get-property target prop-name))
(prop-type (edraw-svg-prop-info-type prop-info))
(prop-number-p (edraw-svg-prop-info-number-p prop-info))
(indent (+ (if (bolp) margin-left 1)
(- name-column-width
(string-width
(edraw-property-editor-property-display-name
prop-name))))))
(cond
(prop-number-p
(edraw-property-editor-create-number-widget
indent target prop-name prop-value prop-info notify))
((eq (car-safe prop-type) 'or)
(edraw-property-editor-create-menu-choice-widget
indent target prop-name prop-value prop-info notify))
((eq prop-type 'paint)
(edraw-property-editor-create-paint-widget
indent target prop-name prop-value prop-info notify
(oref pedit options)))
((eq prop-type 'marker)
(edraw-property-editor-create-marker-widget
(+ margin-left name-column-width)
indent target prop-name prop-value prop-info notify
pedit))
((eq (car-safe prop-type) 'cssdecls)
(edraw-property-editor-create-cssdecls-widget
(+ margin-left name-column-width)
indent target prop-name prop-value prop-info notify
pedit))
((eq prop-type 'text)
(edraw-property-editor-create-text-field-widget
indent target prop-name prop-value prop-info notify
t))
(t
(edraw-property-editor-create-text-field-widget
indent target prop-name prop-value prop-info notify)))))
(defun edraw-property-editor-property-display-name (prop-name)
"Return display name of the property PROP-NAME on property editor
as a string."
(let ((str (format "%s" prop-name))
(edraw-prefix "data-edraw-"))
(if (string-prefix-p edraw-prefix str)
(concat "(ed)" (substring str (length edraw-prefix)))
str)))
;;;;;; Menu Choice Widget
(defun edraw-property-editor-create-menu-choice-widget (indent
target
prop-name prop-value
prop-info notify)
(widget-insert (make-string indent ? ))
(let* ((prop-required (edraw-svg-prop-info-required-p prop-info))
(prop-type (edraw-svg-prop-info-type prop-info))
(types (if prop-required
(cdr prop-type) ;;skip (or)
;; nullable
(cons nil (cdr prop-type))))
(widget
(apply
#'widget-create
`(menu-choice
:button-prefix widget-push-button-prefix
:button-suffix widget-push-button-suffix
:format ,(format
"%s: %%[%s%%] %%v"
(edraw-property-editor-property-display-name prop-name)
(edraw-msg "Choose"))
:value ,(edraw-property-editor-prop-value-to-widget-value
prop-value prop-info)
:notify ,notify
,@(mapcar
(lambda (item)
(cond
((null item) (list 'item :tag " " :value nil)) ;;If :tag="", show separator
((stringp item) (list 'item :tag item :value item))
;;((symbolp item) (list 'editable-field :tag (symbol-name item)))
))
types)))))
(edraw-property-editor-prop-widget
:widget widget
:target target
:prop-info prop-info)))
;;;;;; Text Field Widget
(defun edraw-property-editor-create-text-field-widget (indent
target
prop-name prop-value
prop-info notify
&optional
multiline)
(let* ((disp-name (edraw-property-editor-property-display-name prop-name))
(field-indent (make-string (+ indent (length disp-name) 2) ? ))
(_ (widget-insert (make-string indent ? )))
(widget (widget-create
'editable-field
:keymap (if multiline
edraw-property-editor-text-map
edraw-property-editor-field-map)
:format (format "%s: %%v" disp-name)
:value (edraw-property-editor-prop-value-to-widget-value
prop-value prop-info)
:edraw-indent field-indent
:notify notify))
(prop-widget (edraw-property-editor-prop-widget
:widget widget
:target target
:prop-info prop-info)))
;; Set line-prefix and wrap-prefix for multiline indentation
(let ((beg (1- (widget-field-start widget))) ;;Before value part
(end (1+ (widget-field-end widget)))) ;;Include \n
(put-text-property beg end 'line-prefix field-indent)
(put-text-property beg end 'wrap-prefix field-indent))
;; Return prop widget object
prop-widget))
;;;;;; Number Widget
(defvar edraw-property-editor-number-title-keymap
(let ((km (make-sparse-keymap)))
(define-key km [down-mouse-1] 'edraw-property-editor-number-dragging)
(define-key km [wheel-down] 'edraw-property-editor-field-wheel-decrease)
(define-key km [wheel-up] 'edraw-property-editor-field-wheel-increase)
km))
(defun edraw-property-editor-create-number-widget (indent
target
prop-name prop-value
prop-info notify)
(let* ((line-begin (line-beginning-position))
(name-begin (point))
(name-end
(progn
(widget-insert
(format "%s%s: "
(make-string indent ? )
(edraw-property-editor-property-display-name prop-name)))
(point)))
(widget (widget-create
'editable-field
:keymap edraw-property-editor-field-map
:value (edraw-property-editor-prop-value-to-widget-value
prop-value prop-info)
:notify notify))
(field (edraw-property-editor-number-field-create
(current-buffer) widget prop-info)))
(widget-put widget :edraw-field field)