File size: 9,234 Bytes
3bb13c3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tkinter as tk
from tkinter import font as tkfont
from tkinter import ttk
import logging

logging.basicConfig(level=logging.INFO)

class AppearanceManager:
    def __init__(self):
        self.bg_color = "#2c2c2c"
        self.fg_color = "white"
        self.highlight_color = "yellow"
        self.opacity = 0.9
        self.font_name = "Nasalization Rg"
        self.font_size = 12
        self.letter_spacing = 3
        self.button_border_width = 1
        self.button_style = {
            "bg": self.bg_color,
            "fg": self.fg_color,
            "relief": tk.FLAT,
            "bd": self.button_border_width,
            "highlightthickness": self.button_border_width,
            "highlightbackground": self.fg_color,
            "activebackground": self.bg_color,
            "activeforeground": self.fg_color,
        }
        self.custom_font = None

    # เพิ่มเมธอดในคลาส AppearanceManager:
    def update_bg_color(self, new_color):
        """Update background color and related styles"""
        self.bg_color = new_color
        self.button_style.update({
            "bg": new_color,
            "activebackground": new_color
        })
        
    def apply_bg_color(self, widget):
        """Apply current background color to widget"""
        widget.configure(bg=self.bg_color)    

    def load_custom_font(self, root):
        try:
            self.custom_font = tkfont.Font(family=self.font_name, size=self.font_size)
            logging.info(f"Font loaded successfully: {self.font_name}")
            return self.custom_font
        except Exception as e:
            logging.warning(f"Failed to load font: {e}. Using system default.")
            self.custom_font = tkfont.Font(family="TkDefaultFont", size=self.font_size)
            return self.custom_font

    def apply_style(self, window):
        window.configure(bg=self.bg_color)
        window.attributes('-alpha', self.opacity)
        self.custom_font = self.load_custom_font(window)
        self.button_style["font"] = self.custom_font

        style = ttk.Style()
        style.configure('Switch.TCheckbutton',
                        background=self.bg_color,
                        foreground=self.fg_color,
                        font=self.custom_font)
        style.map('Switch.TCheckbutton',
                background=[('active', self.bg_color)],
                foreground=[('active', self.fg_color)])

        return self.custom_font

    def create_styled_button(self, parent, text, command=None, hover_bg=None):
        text = text.upper()
        spaced_text = " ".join(text)
        button = tk.Button(parent, text=spaced_text, command=command, **self.button_style)
        original_bg = self.button_style['bg']
        hover_bg = hover_bg if hover_bg else original_bg
        button.bind("<Enter>", lambda e: button.config(bg=hover_bg))
        button.bind("<Leave>", lambda e: button.config(bg=original_bg))
        return button

    def create_styled_entry(self, parent, **kwargs):
        entry = tk.Entry(parent, bg=self.bg_color, fg=self.fg_color, 
                        insertbackground=self.fg_color, font=self.custom_font, **kwargs)
        return entry

    def create_styled_label(self, parent, text, **kwargs):
        label = tk.Label(parent, text=text, bg=self.bg_color, fg=self.fg_color, 
                        font=self.custom_font, **kwargs)
        return label

    def create_styled_text(self, parent, **kwargs):
        text_widget = tk.Text(parent, bg=self.bg_color, fg=self.fg_color, 
                            insertbackground=self.fg_color, font=self.custom_font, **kwargs)
        text_widget.tag_configure("highlight", background=self.highlight_color)
        return text_widget

    def create_styled_scale(self, parent, **kwargs):
        """

        สร้าง scale widget พร้อม frame และ value label

        

        Returns:

            tuple: (frame, scale) โดย

                  frame คือ container ที่มี scale และ label

                  scale คือ ttk.Scale widget ที่ใช้ควบคุมค่า

        """
        frame = tk.Frame(parent, bg=self.bg_color)
        digits = kwargs.pop('digits', 0)
        
        style = ttk.Style()
        style.configure("Custom.Horizontal.TScale", 
                       background=self.bg_color, 
                       troughcolor=self.lighten_color(self.bg_color))
        
        scale = ttk.Scale(frame, style="Custom.Horizontal.TScale", **kwargs)
        scale.pack(side=tk.LEFT, fill=tk.X, expand=True)

        value_var = tk.StringVar()
        value_label = tk.Label(frame, textvariable=value_var, 
                             bg=self.bg_color, fg=self.fg_color, width=5)
        value_label.pack(side=tk.RIGHT, padx=(5, 0))

        def update_value(event=None):
            value = scale.get()
            value_var.set(f"{value:.{digits}f}")

        scale.bind("<Motion>", update_value)
        scale.bind("<ButtonRelease-1>", update_value)
        update_value()

        return (frame, scale)

    def create_advance_ui_frame(self, parent):
        frame = tk.Frame(parent, bg=self.bg_color)
        return frame

    def create_api_parameter_form(self, parent):
        form_frame = tk.Frame(parent, bg=self.bg_color)
        
        model_frame = tk.Frame(form_frame, bg=self.bg_color)
        model_frame.pack(fill=tk.X, pady=5)
        model_label = self.create_styled_label(model_frame, "Model:")
        model_label.pack(side=tk.LEFT)
        model_combo = self.create_styled_combobox(model_frame, values=["gpt-4o", "gpt-4o-mini", "claude-3-haiku"])
        model_combo.pack(side=tk.RIGHT, expand=True, fill=tk.X)

        max_tokens_frame = tk.Frame(form_frame, bg=self.bg_color)
        max_tokens_frame.pack(fill=tk.X, pady=5)
        max_tokens_label = self.create_styled_label(max_tokens_frame, "Max Tokens:")
        max_tokens_label.pack(side=tk.LEFT)
        max_tokens_tuple = self.create_styled_scale(max_tokens_frame, from_=100, to=1000, orient=tk.HORIZONTAL)
        max_tokens_tuple[0].pack(side=tk.RIGHT, expand=True, fill=tk.X)

        temp_frame = tk.Frame(form_frame, bg=self.bg_color)
        temp_frame.pack(fill=tk.X, pady=5)
        temp_label = self.create_styled_label(temp_frame, "Temperature:")
        temp_label.pack(side=tk.LEFT)
        temp_tuple = self.create_styled_scale(temp_frame, from_=0.5, to=0.9, orient=tk.HORIZONTAL, digits=2)
        temp_tuple[0].pack(side=tk.RIGHT, expand=True, fill=tk.X)
        
        top_p_frame = tk.Frame(form_frame, bg=self.bg_color)
        top_p_frame.pack(fill=tk.X, pady=5)
        top_p_label = self.create_styled_label(top_p_frame, "Top P:")
        top_p_label.pack(side=tk.LEFT)
        top_p_tuple = self.create_styled_scale(top_p_frame, from_=0.5, to=0.9, orient=tk.HORIZONTAL, digits=2)
        top_p_tuple[0].pack(side=tk.RIGHT, expand=True, fill=tk.X)
        
        # Store references
        form_frame.model_combo = model_combo
        form_frame.max_tokens_scale = max_tokens_tuple[1]
        form_frame.temp_scale = temp_tuple[1]
        form_frame.top_p_scale = top_p_tuple[1]
        
        # Hide top_p for Claude
        def on_model_change(event):
            is_claude = model_combo.get() == "claude-3-haiku"
            if is_claude:
                top_p_frame.pack_forget()
            else:
                top_p_frame.pack(fill=tk.X, pady=5)
        
        model_combo.bind('<<ComboboxSelected>>', on_model_change)
        return form_frame

    def create_save_button(self, parent, command):
        return self.create_styled_button(parent, "Save", command)

    def create_parameter_description(self, parent):
        desc_frame = tk.Frame(parent, bg=self.bg_color)
        
        descriptions = {
            "Model": "โมเดล AI ที่ใช้ในการแปลภาษา",
            "Max Tokens": "จำนวนโทเค็นสูงสุดที่ใช้ในการแปล (100-1000)",
            "Temperature": "ความสร้างสรรค์ในการแปล (0.5-0.9)",
            "Top P": "ความหลากหลายของคำศัพท์ (0.5-0.9)"
        }

        for i, (param, desc) in enumerate(descriptions.items()):
            label = self.create_styled_label(desc_frame, f"{param}: {desc}")
            label.pack(anchor="w", pady=2)

        return desc_frame
    
    def lighten_color(self, color, factor=1.3):
        r = int(color[1:3], 16)
        g = int(color[3:5], 16)
        b = int(color[5:7], 16)
        r = min(int(r * factor), 255)
        g = min(int(g * factor), 255)
        b = min(int(b * factor), 255)
        return f"#{r:02x}{g:02x}{b:02x}"

    def create_styled_combobox(self, parent, **kwargs):
        combo = ttk.Combobox(parent, **kwargs)
        combo.configure(style='Custom.TCombobox')
        return combo

appearance_manager = AppearanceManager()