MagiciteBabel / bg_swatch.py
iarcanar's picture
Upload 23 files
3bb13c3 verified
import tkinter as tk
from PIL import Image, ImageTk
import logging
from appearance import appearance_manager
class BGSwatch:
def __init__(self, root, main_frame, settings):
self.root = root
self.main_frame = main_frame
self.settings = settings
self.current_mode = 2 # เริ่มต้นที่สถานะปิด
self.bg_colors = ['#000000', '#001F3F', None]
self.current_transparency = self.settings.get('bg_swatch_transparency', 0.5)
self.is_translation_active = True # เพิ่มตัวแปรติดตามสถานะการแปล
self.setup_bg_layer()
self.setup_swatch_button()
logging.info("BGSwatch initialized in OFF state")
def on_main_frame_configure(self, event=None):
"""อัพเดทขนาดเมื่อ main_frame มีการเปลี่ยนแปลง"""
self.update_bg_position()
def setup_bg_layer(self):
"""Create and setup the background layer"""
self.bg_window = tk.Toplevel(self.root)
self.bg_window.withdraw()
self.bg_window.overrideredirect(True)
# ตั้งค่าให้อยู่บนโปรแกรมอื่นแต่อยู่ใต้ main UI
self.bg_window.attributes('-topmost', True)
self.bg_window.attributes('-alpha', self.current_transparency)
self.bg_frame = tk.Frame(
self.bg_window,
bg=self.bg_colors[self.current_mode] if self.bg_colors[self.current_mode] else appearance_manager.bg_color
)
self.bg_frame.pack(fill=tk.BOTH, expand=True)
def update_bg_position(self):
"""อัพเดทตำแหน่งและขนาดของพื้นหลัง"""
if not hasattr(self, 'bg_window') or not self.main_frame.winfo_exists():
return
try:
width = self.settings.get('width', 400)
height = self.settings.get('height', 300)
x = self.main_frame.winfo_rootx()
y = self.main_frame.winfo_rooty()
self.bg_window.geometry(f"{width}x{height}+{x}+{y}")
if self.bg_colors[self.current_mode] is not None and self.is_translation_active:
self.bg_window.deiconify()
self.main_frame.lift() # ย้าย main_frame ขึ้นบนสุดเสมอ
self.bg_window.lift() # ย้าย bg_window ขึ้นมาแต่อยู่ใต้ main_frame
self.main_frame.lift() # ยืนยันว่า main_frame อยู่บนสุด
except tk.TclError:
pass
def handle_translation_state(self, is_active):
"""จัดการสถานะ bg-swatch ตาม translation state"""
self.is_translation_active = is_active
if not is_active:
# ถ้าการแปลหยุด ให้ซ่อน bg-swatch และเปลี่ยนโหมดเป็นปิด
self.bg_window.withdraw()
self.current_mode = 2 # โหมดปิด
self.settings.set('bg_swatch_mode', self.current_mode)
logging.info("BG swatch hidden due to translation stop")
else:
# ถ้าการแปลเริ่มใหม่ ยังคงอยู่ในโหมดปิดจนกว่าจะกดเปิดเอง
pass
def setup_swatch_button(self):
"""Create and setup the swatch toggle button"""
# ขยับปุ่มลงอีก 10px (เปลี่ยนจาก y=60 เป็น y=70)
circle_size = 20
circle_image = Image.new('RGBA', (circle_size, circle_size))
for x in range(circle_size):
for y in range(circle_size):
dx = x - circle_size/2
dy = y - circle_size/2
distance = (dx**2 + dy**2)**0.5
if distance <= circle_size/2:
if distance <= (circle_size/2 - 1):
alpha = 255
else:
alpha = int(255 * (1 - (distance - (circle_size/2 - 1))))
circle_image.putpixel((x, y), (255, 255, 255, alpha))
else:
circle_image.putpixel((x, y), (0, 0, 0, 0))
self.swatch_image = ImageTk.PhotoImage(circle_image)
self.swatch_button = tk.Button(
self.main_frame,
image=self.swatch_image,
command=self.toggle_bg_mode,
bg=appearance_manager.bg_color,
bd=0,
cursor="hand2",
activebackground=appearance_manager.bg_color,
highlightthickness=0
)
self.swatch_button.place(relx=1.0, rely=0, anchor="ne", x=0, y=70) # ขยับลง 10px
self.create_tooltip()
def create_tooltip(self):
"""Create tooltip for the swatch button"""
self.tooltip = None
self.swatch_button.bind('<Enter>', self.show_tooltip)
self.swatch_button.bind('<Leave>', self.hide_tooltip)
def show_tooltip(self, event=None):
"""Show tooltip when hovering"""
x, y, _, _ = self.swatch_button.bbox("insert")
x += self.swatch_button.winfo_rootx() + 25
y += self.swatch_button.winfo_rooty() + 20
self.tooltip = tk.Toplevel(self.swatch_button)
self.tooltip.wm_overrideredirect(True)
self.tooltip.wm_geometry(f"+{x}+{y}")
label = tk.Label(
self.tooltip,
text="BG-Swatch",
justify=tk.LEFT,
background="#ffffff",
foreground="#000000",
relief=tk.SOLID,
borderwidth=1,
font=("TkDefaultFont", "8", "normal")
)
label.pack()
def hide_tooltip(self, event=None):
"""Hide tooltip"""
if self.tooltip:
self.tooltip.destroy()
self.tooltip = None
def toggle_bg_mode(self):
"""Toggle between different background modes"""
if not self.is_translation_active:
return # ไม่ให้เปิดถ้าการแปลไม่ทำงาน
self.current_mode = (self.current_mode + 1) % len(self.bg_colors)
if self.bg_colors[self.current_mode] is None:
self.bg_window.withdraw()
logging.info("BG swatch hidden")
else:
self.bg_frame.configure(bg=self.bg_colors[self.current_mode])
self.bg_window.attributes('-alpha', self.current_transparency)
self.update_bg_position()
logging.info(f"BG swatch shown in mode: {self.current_mode}")
self.settings.set('bg_swatch_mode', self.current_mode)
def update_transparency(self, value):
"""Update background transparency (called from settings)"""
self.current_transparency = float(value)
if hasattr(self, 'bg_window') and self.bg_colors[self.current_mode] is not None:
try:
self.bg_window.attributes('-alpha', self.current_transparency)
logging.info(f"Background transparency updated to: {self.current_transparency}")
except (ValueError, tk.TclError) as e:
logging.error(f"Error updating background transparency: {str(e)}")
def save_current_state(self):
"""Save current bg-swatch state to settings"""
self.settings.set('bg_swatch_mode', self.current_mode)
def load_saved_state(self):
"""Load saved bg-swatch state from settings"""
saved_mode = self.settings.get('bg_swatch_mode', 0)
if 0 <= saved_mode < len(self.bg_colors):
self.current_mode = saved_mode
if self.bg_colors[self.current_mode] is not None:
self.update_bg_position() # แสดง bg_window ถ้าจำเป็น