| import tkinter as tk
|
| from PIL import Image, ImageTk
|
| from appearance import appearance_manager
|
|
|
|
|
| class MiniUI:
|
| def __init__(self, root, show_main_ui_callback):
|
| self.root = root
|
| self.show_main_ui_callback = show_main_ui_callback
|
| self.blink_interval = 500
|
| self.blink_timer = None
|
| self.mini_ui = None
|
| self.blink_icon = None
|
| self.black_icon = None
|
| self.mini_ui_blink_label = None
|
| self.mini_ui_blinking = False
|
| self.is_translating = False
|
| self.toggle_translation_callback = None
|
| self.load_icons()
|
| self.create_mini_ui()
|
|
|
| def load_icons(self):
|
| icon_size = (10, 10)
|
| self.blink_icon = ImageTk.PhotoImage(
|
| Image.open("red_icon.png").resize(icon_size)
|
| )
|
| self.black_icon = ImageTk.PhotoImage(
|
| Image.open("black_icon.png").resize(icon_size)
|
| )
|
|
|
| def create_mini_ui(self):
|
| if self.mini_ui:
|
| self.mini_ui.destroy()
|
|
|
| self.mini_ui = tk.Toplevel(self.root)
|
| self.mini_ui.geometry("120x30")
|
| self.mini_ui.overrideredirect(True)
|
| self.mini_ui.attributes("-topmost", True)
|
| self.mini_ui.configure(bg=appearance_manager.bg_color)
|
| self.mini_ui.withdraw()
|
|
|
|
|
| main_frame = tk.Frame(
|
| self.mini_ui,
|
| bg=appearance_manager.bg_color,
|
| highlightthickness=1,
|
| highlightbackground="#333333",
|
| )
|
| main_frame.pack(expand=True, fill=tk.BOTH, padx=1, pady=1)
|
|
|
|
|
| toggle_button = tk.Button(
|
| main_frame,
|
| text="⇄",
|
| command=self.show_main_ui_callback,
|
| bg=appearance_manager.bg_color,
|
| fg="white",
|
| bd=0,
|
| highlightthickness=0,
|
| font=("Arial", 8),
|
| width=2,
|
| cursor="hand2",
|
| )
|
| toggle_button.pack(side=tk.LEFT, padx=(2, 0))
|
|
|
|
|
| self.start_button = tk.Button(
|
| main_frame,
|
| text="Start",
|
| command=self._handle_toggle_translation,
|
| bg=appearance_manager.bg_color,
|
| fg="white",
|
| bd=0,
|
| highlightthickness=0,
|
| font=("FC Minimal", 10, "bold"),
|
| activebackground="#404040",
|
| activeforeground="white",
|
| width=8,
|
| cursor="hand2",
|
| )
|
| self.start_button.pack(side=tk.LEFT, padx=5)
|
|
|
|
|
| self.mini_ui_blink_label = tk.Label(
|
| main_frame, image=self.black_icon, bg=appearance_manager.bg_color
|
| )
|
| self.mini_ui_blink_label.pack(side=tk.RIGHT, padx=5)
|
|
|
|
|
| def on_enter(e):
|
| if not self.is_translating:
|
| self.start_button.config(bg="#666666")
|
| else:
|
| self.start_button.config(bg="#404040")
|
|
|
| def on_leave(e):
|
| self.start_button.config(bg=appearance_manager.bg_color)
|
|
|
|
|
| def on_toggle_enter(e):
|
| toggle_button.config(bg="#666666")
|
|
|
| def on_toggle_leave(e):
|
| toggle_button.config(bg=appearance_manager.bg_color)
|
|
|
| self.start_button.bind("<Enter>", on_enter)
|
| self.start_button.bind("<Leave>", on_leave)
|
| toggle_button.bind("<Enter>", on_toggle_enter)
|
| toggle_button.bind("<Leave>", on_toggle_leave)
|
|
|
|
|
| self.mini_ui.bind("<Button-1>", self.start_move_mini_ui)
|
| self.mini_ui.bind("<B1-Motion>", self.do_move_mini_ui)
|
| self.mini_ui.bind("<Double-Button-1>", lambda e: self.show_main_ui_from_mini())
|
|
|
| def _handle_toggle_translation(self):
|
| """จัดการการกดปุ่ม Start/Stop โดยเรียกใช้ callback จาก main UI"""
|
| if self.toggle_translation_callback:
|
| self.toggle_translation_callback()
|
|
|
| def update_translation_status(self, is_translating):
|
| """อัพเดทสถานะการแปลและ UI"""
|
| self.is_translating = is_translating
|
| self.mini_ui_blinking = is_translating
|
|
|
| if is_translating:
|
| self.start_button.config(text="Stop", bg=appearance_manager.bg_color)
|
| self.start_blinking()
|
| else:
|
| self.start_button.config(text="Start", bg=appearance_manager.bg_color)
|
| self.stop_blinking()
|
|
|
| def set_toggle_translation_callback(self, callback):
|
| """ตั้งค่า callback สำหรับการ toggle การแปล"""
|
| self.toggle_translation_callback = callback
|
|
|
| def start_blinking(self):
|
| """เริ่มการกระพริบของไฟสถานะ"""
|
| if self.blink_timer is None:
|
| self.blink_mini_ui()
|
|
|
| def stop_blinking(self):
|
| """หยุดการกระพริบของไฟสถานะ"""
|
| if self.blink_timer is not None:
|
| self.mini_ui.after_cancel(self.blink_timer)
|
| self.blink_timer = None
|
| if hasattr(self, "mini_ui_blink_label"):
|
| self.mini_ui_blink_label.config(image=self.black_icon)
|
|
|
| def start_move_mini_ui(self, event):
|
| """เริ่มการเคลื่อนย้ายหน้าต่าง"""
|
| self.mini_x = event.x_root - self.mini_ui.winfo_x()
|
| self.mini_y = event.y_root - self.mini_ui.winfo_y()
|
|
|
| def do_move_mini_ui(self, event):
|
| """ทำการเคลื่อนย้ายหน้าต่าง"""
|
| x = event.x_root - self.mini_x
|
| y = event.y_root - self.mini_y
|
| self.mini_ui.geometry(f"+{x}+{y}")
|
|
|
| def show_main_ui_from_mini(self):
|
| """สลับกลับไปแสดง main UI"""
|
| if hasattr(self, "mini_ui"):
|
| self.mini_ui.withdraw()
|
| self.show_main_ui_callback()
|
|
|
| def position_at_center_of_main(self, main_x, main_y, main_width, main_height):
|
| """จัดตำแหน่ง mini UI ให้อยู่กลาง main UI"""
|
| mini_width = 120
|
| mini_height = 30
|
| x = main_x + (main_width - mini_width) // 2
|
| y = main_y + (main_height - mini_height) // 2
|
| self.mini_ui.geometry(f"+{x}+{y}")
|
|
|
| def blink_mini_ui(self):
|
| """จัดการการกระพริบของไฟสถานะ"""
|
| if self.mini_ui_blinking and hasattr(self, "mini_ui") and self.mini_ui:
|
| if hasattr(self, "mini_ui_blink_label"):
|
| current_image = self.mini_ui_blink_label.cget("image")
|
| new_image = (
|
| self.blink_icon
|
| if current_image == str(self.black_icon)
|
| else self.black_icon
|
| )
|
| self.mini_ui_blink_label.config(image=new_image)
|
| self.blink_timer = self.mini_ui.after(
|
| self.blink_interval, self.blink_mini_ui
|
| )
|
| else:
|
| self.stop_blinking()
|
|
|