Welcome to mirror list, hosted at ThFree Co, Russian Federation.

space_toolsystem_common.py « bl_ui « startup « scripts « release - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 3e4f7f6fbcbc4bd8425da8a4b5b7b61d48f359d2 (plain)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
# SPDX-License-Identifier: GPL-2.0-or-later

# <pep8 compliant>
import bpy
from bpy.types import (
    Menu,
)

from bpy.app.translations import pgettext_tip as tip_

__all__ = (
    "ToolDef",
    "ToolSelectPanelHelper",
    "activate_by_id",
    "activate_by_id_or_cycle",
    "description_from_id",
    "keymap_from_id",
)

# Support reloading icons.
if "_icon_cache" in locals():
    release = bpy.app.icons.release
    for icon_value in set(_icon_cache.values()):
        if icon_value != 0:
            release(icon_value)
    del release


# (filename -> icon_value) map
_icon_cache = {}


def _keymap_fn_from_seq(keymap_data):

    def keymap_fn(km):
        if keymap_fn.keymap_data:
            from bl_keymap_utils.io import keymap_init_from_data
            keymap_init_from_data(km, keymap_fn.keymap_data)
    keymap_fn.keymap_data = keymap_data
    return keymap_fn


def _item_is_fn(item):
    return (not (type(item) is ToolDef) and callable(item))


from collections import namedtuple
ToolDef = namedtuple(
    "ToolDef",
    (
        # Unique tool name (within space & mode context).
        "idname",
        # The name to display in the interface.
        "label",
        # Description (for tool-tip), when not set, use the description of 'operator',
        # may be a string or a 'function(context, item, key-map) -> string'.
        "description",
        # The name of the icon to use (found in ``release/datafiles/icons``) or None for no icon.
        "icon",
        # An optional cursor to use when this tool is active.
        "cursor",
        # The properties to use for the widget.
        "widget_properties",
        # An optional gizmo group to activate when the tool is set or None for no gizmo.
        "widget",
        # Optional key-map for tool, possible values are:
        #
        # - ``None`` when the tool doesn't have a key-map.
        #   Also the default value when no key-map value is defined.
        #
        # - A string literal for the key-map name, the key-map items are located in the default key-map.
        #
        # - ``()`` an empty tuple for a default name.
        #   This is convenience functionality for generating a key-map name.
        #   So if a tool name is "Bone Size", in "Edit Armature" mode for the "3D View",
        #   All of these values are combined into an id, e.g:
        #     "3D View Tool: Edit Armature, Bone Envelope"
        #
        #   Typically searching for a string ending with the tool name
        #   in the default key-map will lead you to the key-map for a tool.
        #
        # - A function that populates a key-maps passed in as an argument.
        #
        # - A tuple filled with triple's of:
        #   ``(operator_id, operator_properties, keymap_item_args)``.
        #
        #   Use this to define the key-map in-line.
        #
        #   Note that this isn't used for Blender's built in tools which use the built-in key-map.
        #   Keep this functionality since it's likely useful for add-on key-maps.
        #
        # Warning: currently 'from_dict' this is a list of one item,
        # so internally we can swap the key-map function for the key-map itself.
        # This isn't very nice and may change, tool definitions shouldn't care about this.
        "keymap",
        # Optional data-block associated with this tool.
        # (Typically brush name, usage depends on mode, we could use for non-brush ID's in other modes).
        "data_block",
        # Optional primary operator (for introspection only).
        "operator",
        # Optional draw settings (operator options, tool_settings).
        "draw_settings",
        # Optional draw cursor.
        "draw_cursor",
        # Various options, see: `bpy.types.WorkSpaceTool.setup` options argument.
        "options",
    )
)
del namedtuple


def from_dict(kw_args):
    """
    Use so each tool can avoid defining all members of the named tuple.
    Also convert the keymap from a tuple into a function
    (since keymap is a callback).
    """
    kw = {
        "description": None,
        "icon": None,
        "cursor": None,
        "options": None,
        "widget": None,
        "widget_properties": None,
        "keymap": None,
        "data_block": None,
        "operator": None,
        "draw_settings": None,
        "draw_cursor": None,
    }
    kw.update(kw_args)

    keymap = kw["keymap"]
    if keymap is None:
        pass
    elif type(keymap) is tuple:
        keymap = [_keymap_fn_from_seq(keymap)]
    else:
        keymap = [keymap]
    kw["keymap"] = keymap
    return ToolDef(**kw)


def from_fn(fn):
    """
    Use as decorator so we can define functions.
    """
    return ToolDef.from_dict(fn())


def with_args(**kw):
    def from_fn(fn):
        return ToolDef.from_dict(fn(**kw))
    return from_fn


from_fn.with_args = with_args
ToolDef.from_dict = from_dict
ToolDef.from_fn = from_fn
del from_dict, from_fn, with_args


class ToolActivePanelHelper:
    # Sub-class must define.
    # bl_space_type = 'VIEW_3D'
    # bl_region_type = 'UI'
    bl_label = "Active Tool"
    # bl_category = "Tool"

    def draw(self, context):
        layout = self.layout
        layout.use_property_split = True
        layout.use_property_decorate = False
        ToolSelectPanelHelper.draw_active_tool_header(
            context,
            layout.column(),
            show_tool_icon_always=True,
            tool_key=ToolSelectPanelHelper._tool_key_from_context(context, space_type=self.bl_space_type),
        )


class ToolSelectPanelHelper:
    """
    Generic Class, can be used for any toolbar.

    - keymap_prefix:
      The text prefix for each key-map for this spaces tools.
    - tools_all():
      Generator (context_mode, tools) tuple pairs for all tools defined.
    - tools_from_context(context, mode=None):
      A generator for all tools available in the current context.

    Tool Sequence Structure
    =======================

    Sequences of tools as returned by tools_all() and tools_from_context() are comprised of:

    - A `ToolDef` instance (representing a tool that can be activated).
    - None (a visual separator in the tool list).
    - A tuple of `ToolDef` or None values
      (representing a group of tools that can be selected between using a click-drag action).
      Note that only a single level of nesting is supported (groups cannot contain sub-groups).
    - A callable which takes a single context argument and returns a tuple of values described above.
      When the context is None, all potential tools must be returned.
    """

    @classmethod
    def tools_all(cls):
        """
        Return all tools for this toolbar, this must include all available tools ignoring the current context.
        The value is must be a sequence of (mode, tool_list) pairs, where mode may be object-mode edit-mode etc.
        The mode may be None for tool-bars that don't make use of sub-modes.
        """
        raise Exception("Sub-class %r must implement this method!" % cls)

    @classmethod
    def tools_from_context(cls, context, mode=None):
        """
        Return all tools for the current context,
        this result is used at run-time and may filter out tools to display.
        """
        raise Exception("Sub-class %r must implement this method!" % cls)

    @staticmethod
    def _tool_class_from_space_type(space_type):
        return next(
            (cls for cls in ToolSelectPanelHelper.__subclasses__()
             if cls.bl_space_type == space_type),
            None
        )

    @staticmethod
    def _icon_value_from_icon_handle(icon_name):
        import os
        if icon_name is not None:
            assert(type(icon_name) is str)
            icon_value = _icon_cache.get(icon_name)
            if icon_value is None:
                dirname = bpy.utils.system_resource('DATAFILES', path="icons")
                filename = os.path.join(dirname, icon_name + ".dat")
                try:
                    icon_value = bpy.app.icons.new_triangles_from_file(filename)
                except Exception as ex:
                    if not os.path.exists(filename):
                        print("Missing icons:", filename, ex)
                    else:
                        print("Corrupt icon:", filename, ex)
                    # Use none as a fallback (avoids layout issues).
                    if icon_name != "none":
                        icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle("none")
                    else:
                        icon_value = 0
                _icon_cache[icon_name] = icon_value
            return icon_value
        else:
            return 0

    # tool flattening
    #
    # usually 'tools' is already expanded into ToolDef
    # but when registering a tool, this can still be a function
    # (_tools_flatten is usually called with cls.tools_from_context(context)
    # [that already yields from the function])
    # so if item is still a function (e.g._defs_XXX.generate_from_brushes)
    # seems like we cannot expand here (have no context yet)
    # if we yield None here, this will risk running into duplicate tool bl_idname [in register_tool()]
    # but still better than erroring out
    @staticmethod
    def _tools_flatten(tools):
        for item_parent in tools:
            if item_parent is None:
                yield None
            for item in item_parent if (type(item_parent) is tuple) else (item_parent,):
                if item is None or _item_is_fn(item):
                    yield None
                else:
                    yield item

    @staticmethod
    def _tools_flatten_with_tool_index(tools):
        for item_parent in tools:
            if item_parent is None:
                yield None, -1
            i = 0
            for item in item_parent if (type(item_parent) is tuple) else (item_parent,):
                if item is None or _item_is_fn(item):
                    yield None, -1
                else:
                    yield item, i
                    i += 1

    @staticmethod
    def _tools_flatten_with_dynamic(tools, *, context):
        """
        Expands dynamic items, indices aren't aligned with other flatten functions.
        The context may be None, use as signal to return all items.
        """
        for item_parent in tools:
            if item_parent is None:
                yield None
            for item in item_parent if (type(item_parent) is tuple) else (item_parent,):
                if item is None:
                    yield None
                elif _item_is_fn(item):
                    yield from ToolSelectPanelHelper._tools_flatten_with_dynamic(item(context), context=context)
                else:
                    yield item

    @classmethod
    def _tool_get_active(cls, context, space_type, mode, with_icon=False):
        """
        Return the active Python tool definition and icon name.
        """
        tool_active = ToolSelectPanelHelper._tool_active_from_context(context, space_type, mode)
        tool_active_id = getattr(tool_active, "idname", None)
        for item in ToolSelectPanelHelper._tools_flatten(cls.tools_from_context(context, mode)):
            if item is not None:
                if item.idname == tool_active_id:
                    if with_icon:
                        icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle(item.icon)
                    else:
                        icon_value = 0
                    return (item, tool_active, icon_value)
        return None, None, 0

    @classmethod
    def _tool_get_by_id(cls, context, idname):
        """
        Return the active Python tool definition and index (if in sub-group, else -1).
        """
        for item, index in ToolSelectPanelHelper._tools_flatten_with_tool_index(cls.tools_from_context(context)):
            if item is not None:
                if item.idname == idname:
                    return (item, index)
        return None, -1

    @classmethod
    def _tool_get_by_id_active(cls, context, idname):
        """
        Return the active Python tool definition and index (if in sub-group, else -1).
        """
        for item in cls.tools_from_context(context):
            if item is not None:
                if type(item) is tuple:
                    if item[0].idname == idname:
                        index = cls._tool_group_active_get_from_item(item)
                        return (item[index], index)
                else:
                    if item.idname == idname:
                        return (item, -1)
        return None, -1

    @classmethod
    def _tool_get_by_id_active_with_group(cls, context, idname):
        """
        Return the active Python tool definition and index (if in sub-group, else -1).
        """
        for item in cls.tools_from_context(context):
            if item is not None:
                if type(item) is tuple:
                    if item[0].idname == idname:
                        index = cls._tool_group_active_get_from_item(item)
                        return (item[index], index, item)
                else:
                    if item.idname == idname:
                        return (item, -1, None)
        return None, -1, None

    @classmethod
    def _tool_get_group_by_id(cls, context, idname, *, coerce=False):
        """
        Return the group which contains idname, or None.
        """
        for item in cls.tools_from_context(context):
            if item is not None:
                if type(item) is tuple:
                    for subitem in item:
                        if subitem.idname == idname:
                            return item
                else:
                    if item.idname == idname:
                        if coerce:
                            return (item,)
                        else:
                            return None
        return None

    @classmethod
    def _tool_get_by_flat_index(cls, context, tool_index):
        """
        Return the active Python tool definition and index (if in sub-group, else -1).

        Return the index of the expanded list.
        """
        i = 0
        for item, index in ToolSelectPanelHelper._tools_flatten_with_tool_index(cls.tools_from_context(context)):
            if item is not None:
                if i == tool_index:
                    return (item, index)
                i += 1
        return None, -1

    @classmethod
    def _tool_get_active_by_index(cls, context, tool_index):
        """
        Return the active Python tool definition and index (if in sub-group, else -1).

        Return the index of the list without expanding.
        """
        i = 0
        for item in cls.tools_from_context(context):
            if item is not None:
                if i == tool_index:
                    if type(item) is tuple:
                        index = cls._tool_group_active_get_from_item(item)
                        item = item[index]
                    else:
                        index = -1
                    return (item, index)
                i += 1
        return None, -1

    @classmethod
    def _tool_group_active_get_from_item(cls, item):
        index = cls._tool_group_active.get(item[0].idname, 0)
        # Can happen in the case a group is dynamic.
        #
        # NOTE(Campbell): that in this case it's possible the order could change too,
        # So if we want to support this properly we will need to switch away from using
        # an index and instead use an ID.
        # Currently this is such a rare case occurrence that a range check is OK for now.
        if index >= len(item):
            index = 0
        return index

    @classmethod
    def _tool_group_active_set_by_id(cls, context, idname_group, idname):
        item_group = cls._tool_get_group_by_id(context, idname_group, coerce=True)
        if item_group:
            for i, item in enumerate(item_group):
                if item and item.idname == idname:
                    cls._tool_group_active[item_group[0].idname] = i
                    return True
        return False

    @staticmethod
    def _tool_active_from_context(context, space_type, mode=None, create=False):
        if space_type in {'VIEW_3D', 'PROPERTIES'}:
            if mode is None:
                mode = context.mode
            tool = context.workspace.tools.from_space_view3d_mode(mode, create=create)
            if tool is not None:
                tool.refresh_from_context()
                return tool
        elif space_type == 'IMAGE_EDITOR':
            space_data = context.space_data
            if mode is None:
                if space_data is None:
                    mode = 'VIEW'
                else:
                    mode = space_data.mode
            tool = context.workspace.tools.from_space_image_mode(mode, create=create)
            if tool is not None:
                tool.refresh_from_context()
                return tool
        elif space_type == 'NODE_EDITOR':
            space_data = context.space_data
            tool = context.workspace.tools.from_space_node(create=create)
            if tool is not None:
                tool.refresh_from_context()
                return tool
        elif space_type == 'SEQUENCE_EDITOR':
            space_data = context.space_data
            if mode is None:
                mode = space_data.view_type
            tool = context.workspace.tools.from_space_sequencer(mode, create=create)
            if tool is not None:
                tool.refresh_from_context()
                return tool
        return None

    @staticmethod
    def _tool_identifier_from_button(context):
        return context.button_operator.name

    @classmethod
    def _km_action_simple(cls, kc_default, kc, context_descr, label, keymap_fn):
        km_idname = "%s %s, %s" % (cls.keymap_prefix, context_descr, label)
        km = kc.keymaps.get(km_idname)
        km_kwargs = dict(space_type=cls.bl_space_type, region_type='WINDOW', tool=True)
        if km is None:
            km = kc.keymaps.new(km_idname, **km_kwargs)
            keymap_fn[0](km)
        keymap_fn[0] = km.name

        # Ensure we have a default key map, so the add-ons keymap is properly overlayed.
        if kc_default is not kc:
            kc_default.keymaps.new(km_idname, **km_kwargs)

    @classmethod
    def register(cls):
        wm = bpy.context.window_manager
        # Write into defaults, users may modify in preferences.
        kc_default = wm.keyconfigs.default

        # Track which tool-group was last used for non-active groups.
        # Blender stores the active tool-group index.
        #
        # {tool_name_first: index_in_group, ...}
        cls._tool_group_active = {}

        # ignore in background mode
        if kc_default is None:
            return

        for context_mode, tools in cls.tools_all():
            if context_mode is None:
                context_descr = "All"
            else:
                context_descr = context_mode.replace("_", " ").title()

            for item in cls._tools_flatten_with_dynamic(tools, context=None):
                if item is None:
                    continue
                keymap_data = item.keymap
                if keymap_data is None:
                    continue
                if callable(keymap_data[0]):
                    cls._km_action_simple(kc_default, kc_default, context_descr, item.label, keymap_data)

    @classmethod
    def keymap_ui_hierarchy(cls, context_mode):
        # See: bpy_extras.keyconfig_utils

        # Keymaps may be shared, don't show them twice.
        visited = set()

        for context_mode_test, tools in cls.tools_all():
            if context_mode_test == context_mode:
                for item in cls._tools_flatten(tools):
                    if item is None:
                        continue
                    keymap_data = item.keymap
                    if keymap_data is None:
                        continue
                    km_name = keymap_data[0]
                    # print((km.name, cls.bl_space_type, 'WINDOW', []))

                    if km_name in visited:
                        continue
                    visited.add(km_name)

                    yield (km_name, cls.bl_space_type, 'WINDOW', [])
                    # Callable types don't use fall-backs.
                    if isinstance(km_name, str):
                        yield (km_name + " (fallback)", cls.bl_space_type, 'WINDOW', [])

    # -------------------------------------------------------------------------
    # Layout Generators
    #
    # Meaning of received values:
    # - Bool: True for a separator, otherwise False for regular tools.
    # - None: Signal to finish (complete any final operations, e.g. add padding).

    @staticmethod
    def _layout_generator_single_column(layout, scale_y):
        col = layout.column(align=True)
        col.scale_y = scale_y
        is_sep = False
        while True:
            if is_sep is True:
                col = layout.column(align=True)
                col.scale_y = scale_y
            elif is_sep is None:
                yield None
                return
            is_sep = yield col

    @staticmethod
    def _layout_generator_multi_columns(layout, column_count, scale_y):
        scale_x = scale_y * 1.1
        column_last = column_count - 1

        col = layout.column(align=True)

        row = col.row(align=True)

        row.scale_x = scale_x
        row.scale_y = scale_y

        is_sep = False
        column_index = 0
        while True:
            if is_sep is True:
                if column_index != column_last:
                    row.label(text="")
                col = layout.column(align=True)
                row = col.row(align=True)
                row.scale_x = scale_x
                row.scale_y = scale_y
                column_index = 0

            is_sep = yield row
            if is_sep is None:
                if column_index == column_last:
                    row.label(text="")
                    yield None
                    return

            if column_index == column_count:
                column_index = 0
                row = col.row(align=True)
                row.scale_x = scale_x
                row.scale_y = scale_y
            column_index += 1

    @staticmethod
    def _layout_generator_detect_from_region(layout, region, scale_y):
        """
        Choose an appropriate layout for the toolbar.
        """
        # Currently this just checks the width,
        # we could have different layouts as preferences too.
        system = bpy.context.preferences.system
        view2d = region.view2d
        view2d_scale = (
            view2d.region_to_view(1.0, 0.0)[0] -
            view2d.region_to_view(0.0, 0.0)[0]
        )
        width_scale = region.width * view2d_scale / system.ui_scale

        if width_scale > 120.0:
            show_text = True
            column_count = 1
        else:
            show_text = False
            # 2 column layout, disabled
            if width_scale > 80.0:
                column_count = 2
            else:
                column_count = 1

        if column_count == 1:
            ui_gen = ToolSelectPanelHelper._layout_generator_single_column(
                layout, scale_y=scale_y,
            )
        else:
            ui_gen = ToolSelectPanelHelper._layout_generator_multi_columns(
                layout, column_count=column_count, scale_y=scale_y,
            )

        return ui_gen, show_text

    @classmethod
    def draw_cls(cls, layout, context, detect_layout=True, scale_y=1.75):
        # Use a classmethod so it can be called outside of a panel context.

        # XXX, this UI isn't very nice.
        # We might need to create new button types for this.
        # Since we probably want:
        # - tool-tips that include multiple key shortcuts.
        # - ability to click and hold to expose sub-tools.

        space_type = context.space_data.type
        tool_active_id = getattr(
            ToolSelectPanelHelper._tool_active_from_context(context, space_type),
            "idname", None,
        )

        if detect_layout:
            ui_gen, show_text = cls._layout_generator_detect_from_region(layout, context.region, scale_y)
        else:
            ui_gen = ToolSelectPanelHelper._layout_generator_single_column(layout, scale_y)
            show_text = True

        # Start iteration
        ui_gen.send(None)

        for item in cls.tools_from_context(context):
            if item is None:
                ui_gen.send(True)
                continue

            if type(item) is tuple:
                is_active = False
                i = 0
                for i, sub_item in enumerate(item):
                    if sub_item is None:
                        continue
                    is_active = (sub_item.idname == tool_active_id)
                    if is_active:
                        index = i
                        break
                del i, sub_item

                if is_active:
                    # not ideal, write this every time :S
                    cls._tool_group_active[item[0].idname] = index
                else:
                    index = cls._tool_group_active_get_from_item(item)

                item = item[index]
                use_menu = True
            else:
                index = -1
                use_menu = False

            is_active = (item.idname == tool_active_id)
            icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle(item.icon)

            sub = ui_gen.send(False)

            if use_menu:
                sub.operator_menu_hold(
                    "wm.tool_set_by_id",
                    text=item.label if show_text else "",
                    depress=is_active,
                    menu="WM_MT_toolsystem_submenu",
                    icon_value=icon_value,
                ).name = item.idname
            else:
                sub.operator(
                    "wm.tool_set_by_id",
                    text=item.label if show_text else "",
                    depress=is_active,
                    icon_value=icon_value,
                ).name = item.idname
        # Signal to finish any remaining layout edits.
        ui_gen.send(None)

    def draw(self, context):
        self.draw_cls(self.layout, context)

    @staticmethod
    def _tool_key_from_context(context, *, space_type=None):
        if space_type is None:
            space_data = context.space_data
            space_type = space_data.type
        else:
            space_data = None

        if space_type == 'VIEW_3D':
            return space_type, context.mode
        elif space_type == 'IMAGE_EDITOR':
            if space_data is None:
                space_data = context.space_data
            return space_type, space_data.mode
        elif space_type == 'NODE_EDITOR':
            return space_type, None
        elif space_type == 'SEQUENCE_EDITOR':
            return space_type, context.space_data.view_type
        else:
            return None, None

    @staticmethod
    def tool_active_from_context(context):
        space_type = context.space_data.type
        return ToolSelectPanelHelper._tool_active_from_context(context, space_type)

    @staticmethod
    def draw_active_tool_fallback(
            context, layout, tool,
            *,
            is_horizontal_layout=False,
    ):
        idname_fallback = tool.idname_fallback
        space_type = tool.space_type
        cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
        item_fallback, _index = cls._tool_get_by_id(context, idname_fallback)
        if item_fallback is not None:
            draw_settings = item_fallback.draw_settings
            if draw_settings is not None:
                if not is_horizontal_layout:
                    layout.separator()
                draw_settings(context, layout, tool)

    @staticmethod
    def draw_active_tool_header(
            context, layout,
            *,
            show_tool_icon_always=False,
            tool_key=None,
    ):
        if tool_key is None:
            space_type, mode = ToolSelectPanelHelper._tool_key_from_context(context)
        else:
            space_type, mode = tool_key

        if space_type is None:
            return None

        cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
        item, tool, icon_value = cls._tool_get_active(context, space_type, mode, with_icon=True)
        if item is None:
            return None
        # Note: we could show 'item.text' here but it makes the layout jitter when switching tools.
        # Add some spacing since the icon is currently assuming regular small icon size.
        if show_tool_icon_always:
            layout.label(text="    " + item.label, icon_value=icon_value)
            layout.separator()
        else:
            if context.space_data.show_region_toolbar:
                layout.template_icon(icon_value=0, scale=0.5)
            else:
                layout.template_icon(icon_value=icon_value, scale=0.5)
            layout.separator()

        draw_settings = item.draw_settings
        if draw_settings is not None:
            draw_settings(context, layout, tool)

        idname_fallback = tool.idname_fallback
        if idname_fallback and idname_fallback != item.idname:
            tool_settings = context.tool_settings

            # Show popover which looks like an enum but isn't one.
            if tool_settings.workspace_tool_type == 'FALLBACK':
                tool_fallback_id = cls.tool_fallback_id
                item, _select_index = cls._tool_get_by_id_active(context, tool_fallback_id)
                label = item.label
            else:
                label = "Active Tool"

            split = layout.split(factor=0.33)
            row = split.row()
            row.alignment = 'RIGHT'
            row.label(text="Drag:")
            row = split.row()
            row.context_pointer_set("tool", tool)
            row.popover(panel="TOPBAR_PT_tool_fallback", text=label)

        return tool

    # Show a list of tools in the popover.
    @staticmethod
    def draw_fallback_tool_items(layout, context):
        space_type = context.space_data.type
        if space_type == 'PROPERTIES':
            space_type = 'VIEW_3D'

        cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
        tool_fallback_id = cls.tool_fallback_id

        _item, _select_index, item_group = cls._tool_get_by_id_active_with_group(context, tool_fallback_id)

        if item_group is None:
            # Could print comprehensive message - listing available items.
            raise Exception("Fallback tool doesn't exist")

        col = layout.column(align=True)
        tool_settings = context.tool_settings
        col.prop_enum(
            tool_settings,
            "workspace_tool_type",
            value='DEFAULT',
            text="Active Tool",
        )
        is_active_tool = (tool_settings.workspace_tool_type == 'DEFAULT')

        col = layout.column(align=True)
        if is_active_tool:
            index_current = -1
        else:
            index_current = cls._tool_group_active_get_from_item(item_group)

        for i, sub_item in enumerate(item_group):
            is_active = (i == index_current)

            props = col.operator(
                "wm.tool_set_by_id",
                text=sub_item.label,
                depress=is_active,
            )
            props.name = sub_item.idname
            props.as_fallback = True
            props.space_type = space_type

    @staticmethod
    def draw_fallback_tool_items_for_pie_menu(layout, context):
        space_type = context.space_data.type
        if space_type == 'PROPERTIES':
            space_type = 'VIEW_3D'

        cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
        tool_fallback_id = cls.tool_fallback_id

        _item, _select_index, item_group = cls._tool_get_by_id_active_with_group(context, tool_fallback_id)

        if item_group is None:
            # Could print comprehensive message - listing available items.
            raise Exception("Fallback tool doesn't exist")

        # Allow changing the active tool,
        # even though this isn't the purpose of the pie menu
        # it's confusing from a user perspective if we don't allow it.
        is_fallback_group_active = getattr(
            ToolSelectPanelHelper._tool_active_from_context(context, space_type),
            "idname", None,
        ) in (item.idname for item in item_group)

        pie = layout.menu_pie()
        tool_settings = context.tool_settings
        pie.prop_enum(
            tool_settings,
            "workspace_tool_type",
            value='DEFAULT',
            text="Active Tool",
            icon='TOOL_SETTINGS',  # Could use a less generic icon.
        )
        is_active_tool = (tool_settings.workspace_tool_type == 'DEFAULT')

        if is_active_tool:
            index_current = -1
        else:
            index_current = cls._tool_group_active_get_from_item(item_group)
        for i, sub_item in enumerate(item_group):
            is_active = (i == index_current)
            props = pie.operator(
                "wm.tool_set_by_id",
                text=sub_item.label,
                depress=is_active,
                icon_value=ToolSelectPanelHelper._icon_value_from_icon_handle(sub_item.icon),
            )
            props.name = sub_item.idname
            props.space_type = space_type
            if not is_fallback_group_active:
                props.as_fallback = True


# The purpose of this menu is to be a generic popup to select between tools
# in cases when a single tool allows to select alternative tools.
class WM_MT_toolsystem_submenu(Menu):
    bl_label = ""

    @staticmethod
    def _tool_group_from_button(context):
        # Lookup the tool definitions based on the space-type.
        cls = ToolSelectPanelHelper._tool_class_from_space_type(context.space_data.type)
        if cls is not None:
            button_identifier = ToolSelectPanelHelper._tool_identifier_from_button(context)
            for item_group in cls.tools_from_context(context):
                if type(item_group) is tuple:
                    for sub_item in item_group:
                        if (sub_item is not None) and (sub_item.idname == button_identifier):
                            return cls, item_group
        return None, None

    def draw(self, context):
        layout = self.layout
        layout.scale_y = 2.0

        _cls, item_group = self._tool_group_from_button(context)
        if item_group is None:
            # Should never happen, just in case
            layout.label(text="Unable to find toolbar group")
            return

        for item in item_group:
            if item is None:
                layout.separator()
                continue
            icon_value = ToolSelectPanelHelper._icon_value_from_icon_handle(item.icon)
            layout.operator(
                "wm.tool_set_by_id",
                text=item.label,
                icon_value=icon_value,
            ).name = item.idname


def _activate_by_item(context, space_type, item, index, *, as_fallback=False):
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
    tool = ToolSelectPanelHelper._tool_active_from_context(context, space_type, create=True)
    tool_fallback_id = cls.tool_fallback_id

    if as_fallback:
        # To avoid complicating logic too much, isolate all fallback logic to this block.
        # This will set the tool again, using the item for the fallback instead of the primary tool.
        #
        # If this ends up needing to be more complicated,
        # it would be better to split it into a separate function.

        _item, _select_index, item_group = cls._tool_get_by_id_active_with_group(context, tool_fallback_id)

        if item_group is None:
            # Could print comprehensive message - listing available items.
            raise Exception("Fallback tool doesn't exist")
        index_new = -1
        for i, sub_item in enumerate(item_group):
            if sub_item.idname == item.idname:
                index_new = i
                break
        if index_new == -1:
            raise Exception("Fallback tool not found in group")

        cls._tool_group_active[tool_fallback_id] = index_new

        # Done, now get the current tool to replace the item & index.
        tool_active = ToolSelectPanelHelper._tool_active_from_context(context, space_type)
        item, index = cls._tool_get_by_id(context, getattr(tool_active, "idname", None))
    else:
        # Ensure the active fallback tool is read from saved state (even if the fallback tool is not in use).
        stored_idname_fallback = tool.idname_fallback
        if stored_idname_fallback:
            cls._tool_group_active_set_by_id(context, tool_fallback_id, stored_idname_fallback)
        del stored_idname_fallback

    # Find fallback keymap.
    item_fallback = None
    _item, select_index = cls._tool_get_by_id(context, tool_fallback_id)
    if select_index != -1:
        item_fallback, _index = cls._tool_get_active_by_index(context, select_index)
    # End calculating fallback.

    gizmo_group = item.widget or ""

    idname_fallback = (item_fallback and item_fallback.idname) or ""
    keymap_fallback = (item_fallback and item_fallback.keymap and item_fallback.keymap[0]) or ""
    if keymap_fallback:
        keymap_fallback = keymap_fallback + " (fallback)"

    tool.setup(
        idname=item.idname,
        keymap=item.keymap[0] if item.keymap is not None else "",
        cursor=item.cursor or 'DEFAULT',
        options=item.options or set(),
        gizmo_group=gizmo_group,
        data_block=item.data_block or "",
        operator=item.operator or "",
        index=index,
        idname_fallback=idname_fallback,
        keymap_fallback=keymap_fallback,
    )

    if (
            (gizmo_group != "") and
            (props := tool.gizmo_group_properties(gizmo_group))
    ):
        if props is None:
            print("Error:", gizmo_group, "could not access properties!")
        else:
            gizmo_properties = item.widget_properties
            if gizmo_properties is not None:
                if not isinstance(gizmo_properties, list):
                    raise Exception("expected a list, not a %r" % type(gizmo_properties))

                from bl_keymap_utils.io import _init_properties_from_data
                _init_properties_from_data(props, gizmo_properties)

    WindowManager = bpy.types.WindowManager

    handle_map = _activate_by_item._cursor_draw_handle
    handle = handle_map.pop(space_type, None)
    if handle is not None:
        WindowManager.draw_cursor_remove(handle)
    if item.draw_cursor is not None:
        def handle_fn(context, item, tool, xy):
            item.draw_cursor(context, tool, xy)
        handle = WindowManager.draw_cursor_add(handle_fn, (context, item, tool), space_type, 'WINDOW')
        handle_map[space_type] = handle


_activate_by_item._cursor_draw_handle = {}


def activate_by_id(context, space_type, idname, *, as_fallback=False):
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
    if cls is None:
        return False
    item, index = cls._tool_get_by_id(context, idname)
    if item is None:
        return False
    _activate_by_item(context, space_type, item, index, as_fallback=as_fallback)
    return True


def activate_by_id_or_cycle(context, space_type, idname, *, offset=1, as_fallback=False):

    # Only cycle when the active tool is activated again.
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
    item, _index = cls._tool_get_by_id(context, idname)
    if item is None:
        return False

    tool_active = ToolSelectPanelHelper._tool_active_from_context(context, space_type)
    id_active = getattr(tool_active, "idname", None)

    id_current = ""
    for item_group in cls.tools_from_context(context):
        if type(item_group) is tuple:
            index_current = cls._tool_group_active_get_from_item(item_group)
            for sub_item in item_group:
                if sub_item.idname == idname:
                    id_current = item_group[index_current].idname
                    break
            if id_current:
                break

    if id_current == "":
        return activate_by_id(context, space_type, idname)
    if id_active != id_current:
        return activate_by_id(context, space_type, id_current)

    index_found = (tool_active.index + offset) % len(item_group)

    cls._tool_group_active[item_group[0].idname] = index_found

    item_found = item_group[index_found]
    _activate_by_item(context, space_type, item_found, index_found)
    return True


def description_from_id(context, space_type, idname, *, use_operator=True):
    # Used directly for tooltips.
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
    item, _index = cls._tool_get_by_id(context, idname)
    if item is None:
        return False

    # Custom description.
    description = item.description
    if description is not None:
        if callable(description):
            km = _keymap_from_item(context, item)
            return description(context, item, km)
        return tip_(description)

    # Extract from the operator.
    if use_operator:
        operator = item.operator
        if operator is None:
            if item.keymap is not None:
                km = _keymap_from_item(context, item)
                if km is not None:
                    for kmi in km.keymap_items:
                        if kmi.active:
                            operator = kmi.idname
                            break

        if operator is not None:
            import _bpy
            return tip_(_bpy.ops.get_rna_type(operator).description)
    return ""


def item_from_id(context, space_type, idname):
    # Used directly for tooltips.
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
    if cls is None:
        return None
    item, _index = cls._tool_get_by_id(context, idname)
    return item


def item_from_id_active(context, space_type, idname):
    # Used directly for tooltips.
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
    if cls is None:
        return None
    item, _index = cls._tool_get_by_id_active(context, idname)
    return item


def item_from_id_active_with_group(context, space_type, idname):
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
    if cls is None:
        return None
    cls, item, _index = cls._tool_get_by_id_active_with_group(context, idname)
    return item


def item_group_from_id(context, space_type, idname, *, coerce=False):
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
    if cls is None:
        return None
    return cls._tool_get_group_by_id(context, idname, coerce=coerce)


def item_from_flat_index(context, space_type, index):
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
    if cls is None:
        return None
    item, _index = cls._tool_get_by_flat_index(context, index)
    return item


def item_from_index_active(context, space_type, index):
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
    if cls is None:
        return None
    item, _index = cls._tool_get_active_by_index(context, index)
    return item


def keymap_from_id(context, space_type, idname):
    # Used directly for tooltips.
    cls = ToolSelectPanelHelper._tool_class_from_space_type(space_type)
    if cls is None:
        return None
    item, _index = cls._tool_get_by_id(context, idname)
    if item is None:
        return False

    keymap = item.keymap
    # List container of one.
    if keymap:
        return keymap[0]
    return ""


def _keymap_from_item(context, item):
    if item.keymap is not None:
        wm = context.window_manager
        keyconf = wm.keyconfigs.active
        return keyconf.keymaps.get(item.keymap[0])
    return None


classes = (
    WM_MT_toolsystem_submenu,
)

if __name__ == "__main__":  # only for live edit.
    from bpy.utils import register_class
    for cls in classes:
        register_class(cls)