1from fsbc.util import unused
2import fsui
3import fsbc.system
4from .Constants import Constants
5from .skin import Skin
6
7
8class TabPanel(fsui.Panel):
9    def __init__(self, parent, spacing=10):
10        unused(spacing)
11        fsui.Panel.__init__(self, parent, paintable=True)
12        Skin.set_background_color(self)
13        self.layout = fsui.HorizontalLayout()
14        # self.layout.add_spacer(spacing)
15        # self.layout.padding_left = 10
16        # self.layout.padding_right = 10
17
18        # self.set_background_color((0xdd, 0xdd, 0xdd))
19        self.set_min_height(Constants.TAB_HEIGHT)
20
21    def select_tab(self, index):
22        counter = 0
23        for child in self.layout.children:
24            child = child.element
25            if hasattr(child, "type"):
26                if child.type == child.TYPE_TAB:
27                    if counter == index:
28                        child.select()
29                    counter += 1
30
31    def set_selected_tab(self, tab):
32        for child in self.layout.children:
33            child = child.element
34            if hasattr(child, "type"):
35                if child.type == child.TYPE_TAB:
36                    if child == tab:
37                        child.state = child.STATE_SELECTED
38                        child.refresh()
39                    elif child.state == child.STATE_SELECTED:
40                        if child.group_id == tab.group_id:
41                            child.state = child.STATE_NORMAL
42                            child.refresh()
43
44    def add(self, button, expand=False):
45        self.layout.add(button, expand=expand)
46
47    def add_spacer(self, spacer=0, expand=False):
48        self.layout.add_spacer(spacer, 0, expand=expand)
49
50    def on_paint(self):
51        dc = self.create_dc()
52        self.draw_background(self, dc)
53        # self.draw_border(self, dc)
54
55    @classmethod
56    def draw_border(cls, widget, dc):
57        size = widget.size()
58
59        line_color_1 = Skin.get_background_color()
60        if Skin.fws() or Skin.windows_10():
61            from workspace.ui.theme import WorkspaceTheme
62
63            theme = WorkspaceTheme.instance()
64            line_color_1 = theme.title_separator_color
65            line_color_2 = line_color_1
66        elif line_color_1 is not None:
67            line_color_1 = line_color_1.mix(fsui.Color(0xFF, 0xFF, 0xFF))
68            line_color_2 = line_color_1
69        else:
70            line_color_1 = fsui.Color(0xFF, 0xFF, 0xFF, 0xA0)
71            line_color_2 = line_color_1
72
73        # line_color_1 = fsui.Color(0xff, 0x00, 0x00, 0xff)
74        # line_color_2 = fsui.Color(0x00, 0xff, 0x00, 0xff)
75
76        dc.draw_line(0, size[1] - 2, size[0], size[1] - 2, line_color_1)
77        dc.draw_line(0, size[1] - 1, size[0], size[1] - 1, line_color_2)
78
79    @classmethod
80    def draw_background(
81        cls, widget, dc, selected=False, hover=False, button_style=True
82    ):
83        unused(button_style)
84        if selected:
85            cls.draw_selected_tab(widget, dc)
86        else:
87            cls.draw_border(widget, dc)
88
89        size = widget.size()
90        x = 0
91        y = 0
92        w = size[0]
93        h = size[1] - 2
94
95        if Skin.fws() or Skin.windows_10():
96            from workspace.ui.theme import WorkspaceTheme
97
98            theme = WorkspaceTheme.instance()
99            white = fsui.Color(0xFF, 0xFF, 0xFF)
100            if selected:
101                bg_color = theme.window_background
102                bd_color = theme.title_separator_color
103            elif hover:
104                bg_color = theme.window_background.copy().mix(
105                    theme.title_background
106                )
107                bd_color = theme.title_separator_color.copy().mix(
108                    theme.title_background
109                )
110            else:
111                bg_color = theme.title_background
112                bd_color = theme.title_background
113
114            # dc.draw_rectangle(0, 0, w, h, bd_color)
115            if selected or hover:
116                dc.draw_vertical_gradient(0, 0, 2, size[1], white, bd_color)
117                dc.draw_vertical_gradient(
118                    size[0] - 2,
119                    0,
120                    2,
121                    size[1],
122                    theme.title_background,
123                    bd_color,
124                )
125                # dc.draw_rectangle(2, 2, w - 4, h - 2, bg_color)
126                dc.draw_vertical_gradient(
127                    2,
128                    0,
129                    size[0] - 4,
130                    size[1],
131                    theme.title_background,
132                    bg_color,
133                )
134                if hover and not selected:
135                    dc.draw_rectangle(
136                        0, size[1] - 2, size[0], 2, theme.title_separator_color
137                    )
138            else:
139                dc.draw_rectangle(0, 0, w, h, bd_color)
140            return
141
142        if fsbc.system.macosx:
143            # dc.draw_line(0, 0, w, 0, fsui.Color(198, 198, 198))
144            # dc.draw_line(0, 0, w, 0, fsui.Color(188, 188, 188))
145            dc.draw_line(0, 0, w, 0, fsui.Color(248, 248, 248))
146            y += 1
147            h -= 1
148
149        if selected:
150            x += 2
151            w -= 4
152            h += 2
153
154        # if button_style and hover:
155        #     x += 6
156        #     y += 6
157        #     w -= 12
158        #     h -= 12
159
160        color_1 = Skin.get_background_color()
161        if fsbc.system.macosx and False:
162            if selected:
163                color_2 = color_1
164                color_1 = fsui.Color(0xA7, 0xA7, 0xA7)
165            elif hover:
166                color_1 = fsui.Color(0xA7, 0xA7, 0xA7)
167                color_2 = fsui.Color(0xEF, 0xEF, 0xEF)
168            else:
169                color_1 = fsui.Color(0xA7, 0xA7, 0xA7)
170                color_2 = fsui.Color(0xC0, 0xC0, 0xC0)
171        # elif Skin.adwaita():
172        #     color_1 = fsui.Color(0xea, 0xe7, 0xe5)
173        #     color_2 = fsui.Color(0xea, 0xe7, 0xe5)
174        elif color_1 is not None:
175            if selected:
176                color_2 = color_1
177            elif hover:
178                color_2 = color_1.copy().lighten()
179            else:
180                color_2 = color_1.copy().darken(0.08)
181        else:
182            if selected:
183                return
184                # color_1 = fsui.Color(0x00, 0x00, 0x00, 0x00)
185                # color_2 = color_1
186            elif hover:
187                color_1 = fsui.Color(0xFF, 0xFF, 0xFF, 0x00)
188                color_2 = fsui.Color(0xFF, 0xFF, 0xFF, 0x40)
189            else:
190                color_1 = fsui.Color(0x00, 0x00, 0x00, 0x00)
191                color_2 = fsui.Color(0x00, 0x00, 0x00, 0x20)
192        dc.draw_vertical_gradient(x, y, w, h, color_1, color_2)
193
194        if fsbc.system.macosx and False and not selected and not hover:
195            dc.draw_line(
196                x, y + h - 1, x + w, y + h - 1, fsui.Color(0xA8, 0xA8, 0xA8)
197            )
198
199    @classmethod
200    def draw_selected_tab(cls, widget, dc):
201        line_color_1 = Skin.get_background_color()
202        if Skin.fws() or Skin.windows_10():
203            from workspace.ui.theme import WorkspaceTheme
204
205            theme = WorkspaceTheme.instance()
206            line_color_1 = theme.title_separator_color
207            line_color_2 = line_color_1
208        elif fsbc.system.macosx and False:
209            line_color_1 = fsui.Color(0xA7, 0xA7, 0xA7)
210            line_color_2 = Skin.get_background_color().mix(
211                fsui.Color(0xFF, 0xFF, 0xFF)
212            )
213        elif line_color_1 is not None:
214            line_color_2 = Skin.get_background_color().mix(
215                fsui.Color(0xFF, 0xFF, 0xFF)
216            )
217        else:
218            line_color_1 = fsui.Color(0xFF, 0xFF, 0xFF, 0x00)
219            line_color_2 = fsui.Color(0xFF, 0xFF, 0xFF, 0xA0)
220
221        size = widget.size()
222        dc.draw_vertical_gradient(0, 0, 2, size[1], line_color_1, line_color_2)
223        dc.draw_vertical_gradient(
224            size[0] - 2, 0, 2, size[1], line_color_1, line_color_2
225        )
226