The dominant monitor, is the monitor which most of the window appear on. In other words if 1/3 of the window is on monitor A and 2/3 is on monitor B, then the dominant monitor is monitor B. Thereby if the user presses "go into fullscreen", then it would make sense to make it so on monitor B.

This post includes snippets for implementing a WithDominantMonitor trait, along with a CenterWindow (on monitor) and Fullscreen trait, using glfw in Rust.

Dominant Monitor

use std::mem::discriminant;

// glfw = "0.29.0"
use glfw::{Monitor, Window, WindowMode};

pub trait WithDominantMonitor {
    fn with_dominant_monitor<T, F>(&mut self, f: F) -> T
    where
        F: Fn(&mut Self, Option<&Monitor>) -> T;

    fn with_dominant_monitor_mut<T, F>(&mut self, f: F) -> T
    where
        F: FnMut(&mut Self, Option<&Monitor>) -> T;
}

impl WithDominantMonitor for Window {
    fn with_dominant_monitor<T, F>(&mut self, f: F) -> T
    where
        F: Fn(&mut Self, Option<&Monitor>) -> T,
    {
        let mut glfw = self.glfw.clone();

        glfw.with_connected_monitors_mut(|_, monitors| f(self, dominant_monitor(self, monitors)))
    }

    fn with_dominant_monitor_mut<T, F>(&mut self, mut f: F) -> T
    where
        F: FnMut(&mut Self, Option<&Monitor>) -> T,
    {
        let mut glfw = self.glfw.clone();

        glfw.with_connected_monitors_mut(|_, monitors| f(self, dominant_monitor(self, monitors)))
    }
}

fn dominant_monitor<'a>(window: &Window, monitors: &'a [Monitor]) -> Option<&'a Monitor> {
    let (window_x, window_y) = window.get_pos();
    let (window_w, window_h) = window.get_size();

    let mut dominant_monitor = None;
    let mut dominant_area = -1;

    for monitor in monitors {
        if let Some(mode) = monitor.get_video_mode() {
            let (monitor_x, monitor_y) = monitor.get_pos();
            let (monitor_w, monitor_h) = (mode.width as i32, mode.height as i32);

            let area = {
                let area_min_x = window_x.max(monitor_x);
                let area_min_y = window_y.max(monitor_y);

                let area_max_x = (window_x + window_w).min(monitor_x + monitor_w);
                let area_max_y = (window_y + window_h).min(monitor_y + monitor_h);

                (area_max_x - area_min_x) * (area_max_y - area_min_y)
            };

            if area > dominant_area {
                dominant_monitor = Some(monitor);
                dominant_area = area;
            }
        }
    }

    dominant_monitor
}

Example

Using window.with_dominant_monitor, is similar to using glfw.with_primary_monitor.

glfw.with_primary_monitor(|glfw, monitor| {
    if let Some(monitor) = monitor {

    }
});

window.with_dominant_monitor(|window, monitor| {
    if let Some(monitor) = monitor {

    }
});

Center Window

pub trait CenterWindow {
    fn center_on_primary_monitor(&mut self);
    fn center_on_dominant_monitor(&mut self);
}

impl CenterWindow for Window {
    fn center_on_primary_monitor(&mut self) {
        let mut glfw = self.glfw.clone();

        glfw.with_primary_monitor_mut(|_, monitor| {
            if let Some(monitor) = monitor {
                center_window(self, monitor);
            }
        });
    }

    fn center_on_dominant_monitor(&mut self) {
        self.with_dominant_monitor(|window, monitor| {
            if let Some(monitor) = monitor {
                center_window(window, monitor);
            }
        });
    }
}

fn center_window(window: &mut Window, monitor: &Monitor) {
    if let Some(mode) = monitor.get_video_mode() {
        let (monitor_x, monitor_y) = monitor.get_pos();
        let (window_w, window_h) = window.get_size();

        window.set_pos(
            monitor_x + ((mode.width as i32)  - window_w) / 2,
            monitor_y + ((mode.height as i32) - window_h) / 2);
    }
}

Fullscreen

pub trait Fullscreen {
    fn is_fullscreen(&self) -> bool;
    fn set_fullscreen(&mut self, monitor: &Monitor);
}

impl Fullscreen for Window {
    fn is_fullscreen(&self) -> bool {
        self.with_window_mode(|mode| {
            discriminant(&mode) != discriminant(&WindowMode::Windowed)
        })
    }

    fn set_fullscreen(&mut self, monitor: &Monitor) {
        if let Some(mode) = monitor.get_video_mode() {
            self.set_monitor(WindowMode::FullScreen(&monitor), 0, 0, mode.width, mode.height, Some(mode.refresh_rate));
        }
    }
}

Center Window + Fullscreen Example

Store the non-fullscreen window position and size:

let mut window_las_pos = window.get_pos();
let mut window_last_size = window.get_size();

Toggle fullscreen when F11 is pressed.

WindowEvent::Key(Key::F11, _, Action::Press, _) => {
    if window.is_fullscreen() {
        window.set_monitor(WindowMode::Windowed, window_las_pos.0, window_las_pos.1, window_last_size.0 as u32, window_last_size.1 as u32, None);
        window.center_on_dominant_monitor();
    } else {
        window_las_pos = window.get_pos();
        window_last_size = window.get_size();

        window.with_dominant_monitor(|window, monitor| {
            if let Some(monitor) = monitor {
                window.set_fullscreen(monitor);
            }
        });
    }
},