Consolidate window configuration

This commit is contained in:
Grant Sanderson 2024-12-10 10:10:58 -06:00
parent 950ac31b9b
commit 9e77b0dcdd
3 changed files with 43 additions and 38 deletions

View file

@ -319,17 +319,40 @@ def get_window_config(args: Namespace, global_config: dict) -> dict:
except screeninfo.ScreenInfoError: except screeninfo.ScreenInfoError:
# Default fallback # Default fallback
monitors = [screeninfo.Monitor(width=1920, height=1080)] monitors = [screeninfo.Monitor(width=1920, height=1080)]
mon_index = global_config["window_monitor"] mon_index = global_config["window"]["monitor"]
monitor = monitors[min(mon_index, len(monitors) - 1)] monitor = monitors[min(mon_index, len(monitors) - 1)]
width, height = get_resolution(args, global_config) width, height = get_resolution(args, global_config)
aspect_ratio = width / height aspect_ratio = width / height
window_width = monitor.width window_width = monitor.width
if not (args.full_screen or global_config["full_screen"]): if not (args.full_screen or global_config["window"]["full_screen"]):
window_width //= 2 window_width //= 2
window_height = int(window_width / aspect_ratio) window_height = int(window_width / aspect_ratio)
return dict(size=(window_width, window_height))
# Find position (Perhaps factor this out)
pos_str = global_config["window"]["position"]
# Position might be specified with a string of the form
# x,y for integers x and y
if "," in pos_str:
default_position = tuple(map(int, pos_str.split(",")))
else:
# Alternatively, it might be specified with a string like
# UR, OO, DL, etc. specifying what corner it should go to
char_to_n = {"L": 0, "U": 0, "O": 1, "R": 2, "D": 2}
width_diff = monitor.width - window_width
height_diff = monitor.height - window_height
x_step = char_to_n[pos_str[1]] * width_diff // 2
y_step = char_to_n[pos_str[0]] * height_diff // 2
default_position = (
monitor.x + x_step,
-monitor.y + y_step,
)
return dict(
size=(window_width, window_height),
default_position=default_position
)
def get_camera_config(args: Optional[Namespace] = None, global_config: Optional[dict] = None) -> dict: def get_camera_config(args: Optional[Namespace] = None, global_config: Optional[dict] = None) -> dict:

View file

@ -25,18 +25,15 @@ directories:
# it stores this saved data to whatever directory appdirs.user_cache_dir("manim") returns, # it stores this saved data to whatever directory appdirs.user_cache_dir("manim") returns,
# but here a user can specify a different cache location # but here a user can specify a different cache location
cache: "" cache: ""
universal_import_line: "from manimlib import *" window:
style: # Set the position of window on screen, you can use directions, e.g. UL/DR/OL/OO/...
tex_template: "default" # also, you can also specify the position(pixel) of the upper left corner of
font: "Consolas" # the window on the monitor, e.g. "960,540"
text_alignment: "LEFT" position: UR
background_color: "#333333" # If using multiple monitors, which one should show the window?
# Set the position of preview window, you can use directions, e.g. UL/DR/OL/OO/... monitor: 0
# also, you can also specify the position(pixel) of the upper left corner of # If not full screen, the default to give it half the screen width
# the window on the monitor, e.g. "960,540" full_screen: False
window_position: UR
window_monitor: 0
full_screen: False
file_writer_config: file_writer_config:
# If break_into_partial_movies is set to True, then many small # If break_into_partial_movies is set to True, then many small
# files will be written corresponding to each Scene.play and # files will be written corresponding to each Scene.play and
@ -49,6 +46,11 @@ file_writer_config:
pixel_format: "yuv420p" pixel_format: "yuv420p"
saturation: 1.0 saturation: 1.0
gamma: 1.0 gamma: 1.0
style:
tex_template: "default"
font: "Consolas"
text_alignment: "LEFT"
background_color: "#333333"
camera_resolutions: camera_resolutions:
low: "854x480" low: "854x480"
med: "1280x720" med: "1280x720"
@ -56,6 +58,7 @@ camera_resolutions:
4k: "3840x2160" 4k: "3840x2160"
default_resolution: "high" default_resolution: "high"
fps: 30 fps: 30
universal_import_line: "from manimlib import *"
embed_exception_mode: "Verbose" embed_exception_mode: "Verbose"
embed_error_sound: False embed_error_sound: False
ignore_manimlib_modules_on_reload: True ignore_manimlib_modules_on_reload: True

View file

@ -31,13 +31,14 @@ class Window(PygletWindow):
self, self,
scene: Optional[Scene] = None, scene: Optional[Scene] = None,
size: tuple[int, int] = (1280, 720), size: tuple[int, int] = (1280, 720),
default_position: tuple[int, int] = (0, 0),
samples: int = 0 samples: int = 0
): ):
super().__init__(size=size, samples=samples) super().__init__(size=size, samples=samples)
self.scene = scene self.scene = scene
self.default_size = size self.default_size = size
self.default_position = self.find_initial_position(size) self.default_position = default_position
self.pressed_keys = set() self.pressed_keys = set()
self.size = size self.size = size
@ -86,28 +87,6 @@ class Window(PygletWindow):
self.size = (w - 1, h - 1) self.size = (w - 1, h - 1)
self.size = (w, h) self.size = (w, h)
def find_initial_position(self, size: tuple[int, int]) -> tuple[int, int]:
global_config = get_global_config()
custom_position = global_config["window_position"]
mon_index = global_config["window_monitor"]
monitors = get_monitors()
monitor = monitors[min(mon_index, len(monitors) - 1)]
window_width, window_height = size
# Position might be specified with a string of the form
# x,y for integers x and y
if "," in custom_position:
return tuple(map(int, custom_position.split(",")))
# Alternatively, it might be specified with a string like
# UR, OO, DL, etc. specifying what corner it should go to
char_to_n = {"L": 0, "U": 0, "O": 1, "R": 2, "D": 2}
width_diff = monitor.width - window_width
height_diff = monitor.height - window_height
return (
monitor.x + char_to_n[custom_position[1]] * width_diff // 2,
-monitor.y + char_to_n[custom_position[0]] * height_diff // 2,
)
# Delegate event handling to scene # Delegate event handling to scene
def pixel_coords_to_space_coords( def pixel_coords_to_space_coords(
self, self,