Skip to content

Commit d6c423d

Browse files
Osspialfelixrabe
authored andcommitted
Refine function names and type signatures (rust-windowing#886)
* First name consistency pass. More to come! * Remove multitouch variable (hopefully this compiles!) * Remove CreationError::NotSupported * Add new error handling types * Remove `get_` prefix from getters. This is as per the Rust naming conventions recommended in https://rust-lang-nursery.github.io/api-guidelines/naming.html#getter-names-follow-rust-convention-c-getter * Make changes to Window position and size function signatures * Remove CreationError in favor of OsError * Begin updating iOS backend * Change MonitorHandle::outer_position to just position * Fix build on Windows and Linux * Add Display and Error implementations to Error types * Attempt to fix iOS build. I can't actually check that this works since I can't cross-compile to iOS on a Windows machine (thanks apple :/) but this should be one of several commits to get it working. * Attempt to fix iOS errors, and muck up Travis to make debugging easier * More iOS fixins * Add Debug and Display impls to OsError * Fix Display impl * Fix unused code warnings and travis * Rename set_ime_spot to set_ime_position * Add CHANGELOG entry * Rename set_cursor to set_cursor_icon and MouseCursor to CursorIcon * Organize Window functions into multiple, categorized impls * Improve clarity of function ordering and docs in EventLoop
1 parent b2cf8b1 commit d6c423d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+1242
-1243
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
- `LoopDestroyed` is emitted when the `run` or `run_return` method is about to exit.
3838
- Rename `MonitorId` to `MonitorHandle`.
3939
- Removed `serde` implementations from `ControlFlow`.
40+
- Rename several functions to improve both internal consistency and compliance with Rust API guidelines.
41+
- Remove `WindowBuilder::multitouch` field, since it was only implemented on a few platforms. Multitouch is always enabled now.
4042

4143
# Version 0.19.1 (2019-04-08)
4244

examples/cursor.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
extern crate winit;
22

3-
use winit::window::{WindowBuilder, MouseCursor};
3+
use winit::window::{WindowBuilder, CursorIcon};
44
use winit::event::{Event, WindowEvent, ElementState, KeyboardInput};
55
use winit::event_loop::{EventLoop, ControlFlow};
66

@@ -16,7 +16,7 @@ fn main() {
1616
match event {
1717
Event::WindowEvent { event: WindowEvent::KeyboardInput { input: KeyboardInput { state: ElementState::Pressed, .. }, .. }, .. } => {
1818
println!("Setting cursor to \"{:?}\"", CURSORS[cursor_idx]);
19-
window.set_cursor(CURSORS[cursor_idx]);
19+
window.set_cursor_icon(CURSORS[cursor_idx]);
2020
if cursor_idx < CURSORS.len() - 1 {
2121
cursor_idx += 1;
2222
} else {
@@ -32,17 +32,17 @@ fn main() {
3232
});
3333
}
3434

35-
const CURSORS: &[MouseCursor] = &[
36-
MouseCursor::Default, MouseCursor::Crosshair, MouseCursor::Hand,
37-
MouseCursor::Arrow, MouseCursor::Move, MouseCursor::Text,
38-
MouseCursor::Wait, MouseCursor::Help, MouseCursor::Progress,
39-
MouseCursor::NotAllowed, MouseCursor::ContextMenu, MouseCursor::Cell,
40-
MouseCursor::VerticalText, MouseCursor::Alias, MouseCursor::Copy,
41-
MouseCursor::NoDrop, MouseCursor::Grab, MouseCursor::Grabbing,
42-
MouseCursor::AllScroll, MouseCursor::ZoomIn, MouseCursor::ZoomOut,
43-
MouseCursor::EResize, MouseCursor::NResize, MouseCursor::NeResize,
44-
MouseCursor::NwResize, MouseCursor::SResize, MouseCursor::SeResize,
45-
MouseCursor::SwResize, MouseCursor::WResize, MouseCursor::EwResize,
46-
MouseCursor::NsResize, MouseCursor::NeswResize, MouseCursor::NwseResize,
47-
MouseCursor::ColResize, MouseCursor::RowResize
35+
const CURSORS: &[CursorIcon] = &[
36+
CursorIcon::Default, CursorIcon::Crosshair, CursorIcon::Hand,
37+
CursorIcon::Arrow, CursorIcon::Move, CursorIcon::Text,
38+
CursorIcon::Wait, CursorIcon::Help, CursorIcon::Progress,
39+
CursorIcon::NotAllowed, CursorIcon::ContextMenu, CursorIcon::Cell,
40+
CursorIcon::VerticalText, CursorIcon::Alias, CursorIcon::Copy,
41+
CursorIcon::NoDrop, CursorIcon::Grab, CursorIcon::Grabbing,
42+
CursorIcon::AllScroll, CursorIcon::ZoomIn, CursorIcon::ZoomOut,
43+
CursorIcon::EResize, CursorIcon::NResize, CursorIcon::NeResize,
44+
CursorIcon::NwResize, CursorIcon::SResize, CursorIcon::SeResize,
45+
CursorIcon::SwResize, CursorIcon::WResize, CursorIcon::EwResize,
46+
CursorIcon::NsResize, CursorIcon::NeswResize, CursorIcon::NwseResize,
47+
CursorIcon::ColResize, CursorIcon::RowResize
4848
];

examples/cursor_grab.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ fn main() {
2929
use winit::event::VirtualKeyCode::*;
3030
match key {
3131
Escape => *control_flow = ControlFlow::Exit,
32-
G => window.grab_cursor(!modifiers.shift).unwrap(),
33-
H => window.hide_cursor(!modifiers.shift),
32+
G => window.set_cursor_grab(!modifiers.shift).unwrap(),
33+
H => window.set_cursor_visible(modifiers.shift),
3434
_ => (),
3535
}
3636
}

examples/fullscreen.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -82,16 +82,16 @@ fn main() {
8282
if !is_fullscreen {
8383
window.set_fullscreen(None);
8484
} else {
85-
window.set_fullscreen(Some(window.get_current_monitor()));
85+
window.set_fullscreen(Some(window.current_monitor()));
8686
}
8787
}
8888
(VirtualKeyCode::S, ElementState::Pressed) => {
89-
println!("window.get_fullscreen {:?}", window.get_fullscreen());
89+
println!("window.fullscreen {:?}", window.fullscreen());
9090

9191
#[cfg(target_os = "macos")]
9292
{
9393
use winit::platform::macos::WindowExtMacOS;
94-
println!("window.get_simple_fullscreen {:?}", WindowExtMacOS::get_simple_fullscreen(&window));
94+
println!("window.simple_fullscreen {:?}", WindowExtMacOS::simple_fullscreen(&window));
9595
}
9696
}
9797
(VirtualKeyCode::M, ElementState::Pressed) => {
@@ -113,8 +113,8 @@ fn main() {
113113

114114
// Enumerate monitors and prompt user to choose one
115115
fn prompt_for_monitor(event_loop: &EventLoop<()>) -> MonitorHandle {
116-
for (num, monitor) in event_loop.get_available_monitors().enumerate() {
117-
println!("Monitor #{}: {:?}", num, monitor.get_name());
116+
for (num, monitor) in event_loop.available_monitors().enumerate() {
117+
println!("Monitor #{}: {:?}", num, monitor.name());
118118
}
119119

120120
print!("Please write the number of the monitor to use: ");
@@ -123,9 +123,9 @@ fn prompt_for_monitor(event_loop: &EventLoop<()>) -> MonitorHandle {
123123
let mut num = String::new();
124124
io::stdin().read_line(&mut num).unwrap();
125125
let num = num.trim().parse().ok().expect("Please enter a number");
126-
let monitor = event_loop.get_available_monitors().nth(num).expect("Please enter a valid ID");
126+
let monitor = event_loop.available_monitors().nth(num).expect("Please enter a valid ID");
127127

128-
println!("Using {:?}", monitor.get_name());
128+
println!("Using {:?}", monitor.name());
129129

130130
monitor
131131
}

examples/min_max_size.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ fn main() {
1212
.build(&event_loop)
1313
.unwrap();
1414

15-
window.set_min_dimensions(Some(LogicalSize::new(400.0, 200.0)));
16-
window.set_max_dimensions(Some(LogicalSize::new(800.0, 400.0)));
15+
window.set_min_inner_size(Some(LogicalSize::new(400.0, 200.0)));
16+
window.set_max_inner_size(Some(LogicalSize::new(800.0, 400.0)));
1717

1818
event_loop.run(move |event, _, control_flow| {
1919
println!("{:?}", event);

examples/monitor_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ use winit::window::WindowBuilder;
55
fn main() {
66
let event_loop = EventLoop::new();
77
let window = WindowBuilder::new().build(&event_loop).unwrap();
8-
println!("{:#?}\nPrimary: {:#?}", window.get_available_monitors(), window.get_primary_monitor());
8+
println!("{:#?}\nPrimary: {:#?}", window.available_monitors(), window.primary_monitor());
99
}

examples/multithreaded.rs

+17-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{collections::HashMap, sync::mpsc, thread, time::Duration};
55

66
use winit::{
77
event::{ElementState, Event, KeyboardInput, VirtualKeyCode, WindowEvent},
8-
event_loop::{ControlFlow, EventLoop}, window::{MouseCursor, WindowBuilder},
8+
event_loop::{ControlFlow, EventLoop}, window::{CursorIcon, WindowBuilder},
99
};
1010

1111
const WINDOW_COUNT: usize = 3;
@@ -17,7 +17,7 @@ fn main() {
1717
let mut window_senders = HashMap::with_capacity(WINDOW_COUNT);
1818
for _ in 0..WINDOW_COUNT {
1919
let window = WindowBuilder::new()
20-
.with_dimensions(WINDOW_SIZE.into())
20+
.with_inner_size(WINDOW_SIZE.into())
2121
.build(&event_loop)
2222
.unwrap();
2323
let (tx, rx) = mpsc::channel();
@@ -36,31 +36,31 @@ fn main() {
3636
use self::VirtualKeyCode::*;
3737
match key {
3838
A => window.set_always_on_top(state),
39-
C => window.set_cursor(match state {
40-
true => MouseCursor::Progress,
41-
false => MouseCursor::Default,
39+
C => window.set_cursor_icon(match state {
40+
true => CursorIcon::Progress,
41+
false => CursorIcon::Default,
4242
}),
4343
D => window.set_decorations(!state),
4444
F => window.set_fullscreen(match state {
45-
true => Some(window.get_current_monitor()),
45+
true => Some(window.current_monitor()),
4646
false => None,
4747
}),
48-
G => window.grab_cursor(state).unwrap(),
49-
H => window.hide_cursor(state),
48+
G => window.set_cursor_grab(state).unwrap(),
49+
H => window.set_cursor_visible(!state),
5050
I => {
5151
println!("Info:");
52-
println!("-> position : {:?}", window.get_position());
53-
println!("-> inner_position : {:?}", window.get_inner_position());
54-
println!("-> outer_size : {:?}", window.get_outer_size());
55-
println!("-> inner_size : {:?}", window.get_inner_size());
52+
println!("-> outer_position : {:?}", window.outer_position());
53+
println!("-> inner_position : {:?}", window.inner_position());
54+
println!("-> outer_size : {:?}", window.outer_size());
55+
println!("-> inner_size : {:?}", window.inner_size());
5656
},
57-
L => window.set_min_dimensions(match state {
57+
L => window.set_min_inner_size(match state {
5858
true => Some(WINDOW_SIZE.into()),
5959
false => None,
6060
}),
6161
M => window.set_maximized(state),
62-
P => window.set_position({
63-
let mut position = window.get_position().unwrap();
62+
P => window.set_outer_position({
63+
let mut position = window.outer_position().unwrap();
6464
let sign = if state { 1.0 } else { -1.0 };
6565
position.x += 10.0 * sign;
6666
position.y += 10.0 * sign;
@@ -77,9 +77,9 @@ fn main() {
7777
WINDOW_SIZE.1 as i32 / 2,
7878
).into()).unwrap(),
7979
Z => {
80-
window.hide();
80+
window.set_visible(false);
8181
thread::sleep(Duration::from_secs(1));
82-
window.show();
82+
window.set_visible(true);
8383
},
8484
_ => (),
8585
}

examples/resizable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn main() {
1010

1111
let window = WindowBuilder::new()
1212
.with_title("Hit space to toggle resizability.")
13-
.with_dimensions((400, 200).into())
13+
.with_inner_size((400, 200).into())
1414
.with_resizable(resizable)
1515
.build(&event_loop)
1616
.unwrap();

src/dpi.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
//! windows. This event is sent any time the DPI factor changes, either because the window moved to another monitor,
3434
//! or because the user changed the configuration of their screen.
3535
//! - You can also retrieve the DPI factor of a monitor by calling
36-
//! [`MonitorHandle::get_hidpi_factor`](../monitor/struct.MonitorHandle.html#method.get_hidpi_factor), or the
36+
//! [`MonitorHandle::hidpi_factor`](../monitor/struct.MonitorHandle.html#method.hidpi_factor), or the
3737
//! current DPI factor applied to a window by calling
38-
//! [`Window::get_hidpi_factor`](../window/struct.Window.html#method.get_hidpi_factor), which is roughly equivalent
39-
//! to `window.get_current_monitor().get_hidpi_factor()`.
38+
//! [`Window::hidpi_factor`](../window/struct.Window.html#method.hidpi_factor), which is roughly equivalent
39+
//! to `window.current_monitor().hidpi_factor()`.
4040
//!
4141
//! Depending on the platform, the window's actual DPI factor may only be known after
4242
//! the event loop has started and your window has been drawn once. To properly handle these cases,

src/error.rs

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
use std::fmt;
2+
use std::error;
3+
4+
use platform_impl;
5+
6+
/// An error whose cause it outside Winit's control.
7+
#[derive(Debug)]
8+
pub enum ExternalError {
9+
/// The operation is not supported by the backend.
10+
NotSupported(NotSupportedError),
11+
/// The OS cannot perform the operation.
12+
Os(OsError),
13+
}
14+
15+
/// The error type for when the requested operation is not supported by the backend.
16+
#[derive(Clone)]
17+
pub struct NotSupportedError {
18+
_marker: (),
19+
}
20+
21+
/// The error type for when the OS cannot perform the requested operation.
22+
#[derive(Debug)]
23+
pub struct OsError {
24+
line: u32,
25+
file: &'static str,
26+
error: platform_impl::OsError,
27+
}
28+
29+
impl NotSupportedError {
30+
#[inline]
31+
#[allow(dead_code)]
32+
pub(crate) fn new() -> NotSupportedError {
33+
NotSupportedError {
34+
_marker: ()
35+
}
36+
}
37+
}
38+
39+
impl OsError {
40+
#[allow(dead_code)]
41+
pub(crate) fn new(line: u32, file: &'static str, error: platform_impl::OsError) -> OsError {
42+
OsError {
43+
line,
44+
file,
45+
error,
46+
}
47+
}
48+
}
49+
50+
#[allow(unused_macros)]
51+
macro_rules! os_error {
52+
($error:expr) => {{
53+
crate::error::OsError::new(line!(), file!(), $error)
54+
}}
55+
}
56+
57+
impl fmt::Display for OsError {
58+
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
59+
formatter.pad(&format!("os error at {}:{}: {}", self.file, self.line, self.error))
60+
}
61+
}
62+
63+
impl fmt::Display for ExternalError {
64+
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
65+
match self {
66+
ExternalError::NotSupported(e) => e.fmt(formatter),
67+
ExternalError::Os(e) => e.fmt(formatter),
68+
}
69+
}
70+
}
71+
72+
impl fmt::Debug for NotSupportedError {
73+
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
74+
formatter.debug_struct("NotSupportedError").finish()
75+
}
76+
}
77+
78+
impl fmt::Display for NotSupportedError {
79+
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
80+
formatter.pad("the requested operation is not supported by Winit")
81+
}
82+
}
83+
84+
impl error::Error for OsError {}
85+
impl error::Error for ExternalError {}
86+
impl error::Error for NotSupportedError {}

src/event_loop.rs

+20-21
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ impl Default for ControlFlow {
9494

9595
impl EventLoop<()> {
9696
/// Builds a new event loop with a `()` as the user event type.
97-
///
97+
///
9898
/// ## Platform-specific
99-
///
99+
///
100100
/// - **iOS:** Can only be called on the main thread.
101101
pub fn new() -> EventLoop<()> {
102102
EventLoop::<()>::new_user_event()
@@ -110,9 +110,9 @@ impl<T> EventLoop<T> {
110110
/// using an environment variable `WINIT_UNIX_BACKEND`. Legal values are `x11` and `wayland`.
111111
/// If it is not set, winit will try to connect to a wayland connection, and if it fails will
112112
/// fallback on x11. If this variable is set with any other value, winit will panic.
113-
///
113+
///
114114
/// ## Platform-specific
115-
///
115+
///
116116
/// - **iOS:** Can only be called on the main thread.
117117
pub fn new_user_event() -> EventLoop<T> {
118118
EventLoop {
@@ -121,21 +121,6 @@ impl<T> EventLoop<T> {
121121
}
122122
}
123123

124-
/// Returns the list of all the monitors available on the system.
125-
///
126-
// Note: should be replaced with `-> impl Iterator` once stable.
127-
#[inline]
128-
pub fn get_available_monitors(&self) -> AvailableMonitorsIter {
129-
let data = self.event_loop.get_available_monitors();
130-
AvailableMonitorsIter{ data: data.into_iter() }
131-
}
132-
133-
/// Returns the primary monitor of the system.
134-
#[inline]
135-
pub fn get_primary_monitor(&self) -> MonitorHandle {
136-
MonitorHandle { inner: self.event_loop.get_primary_monitor() }
137-
}
138-
139124
/// Hijacks the calling thread and initializes the `winit` event loop with the provided
140125
/// closure. Since the closure is `'static`, it must be a `move` closure if it needs to
141126
/// access any data from the calling context.
@@ -153,13 +138,27 @@ impl<T> EventLoop<T> {
153138
self.event_loop.run(event_handler)
154139
}
155140

156-
/// Creates an `EventLoopProxy` that can be used to wake up the `EventLoop` from another
157-
/// thread.
141+
/// Creates an `EventLoopProxy` that can be used to dispatch user events to the main event loop.
158142
pub fn create_proxy(&self) -> EventLoopProxy<T> {
159143
EventLoopProxy {
160144
event_loop_proxy: self.event_loop.create_proxy(),
161145
}
162146
}
147+
148+
/// Returns the list of all the monitors available on the system.
149+
///
150+
// Note: should be replaced with `-> impl Iterator` once stable.
151+
#[inline]
152+
pub fn available_monitors(&self) -> AvailableMonitorsIter {
153+
let data = self.event_loop.available_monitors();
154+
AvailableMonitorsIter{ data: data.into_iter() }
155+
}
156+
157+
/// Returns the primary monitor of the system.
158+
#[inline]
159+
pub fn primary_monitor(&self) -> MonitorHandle {
160+
MonitorHandle { inner: self.event_loop.primary_monitor() }
161+
}
163162
}
164163

165164
impl<T> Deref for EventLoop<T> {

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,8 @@ extern crate smithay_client_toolkit as sctk;
113113
extern crate calloop;
114114

115115
pub mod dpi;
116+
#[macro_use]
117+
pub mod error;
116118
pub mod event;
117119
pub mod event_loop;
118120
mod icon;

0 commit comments

Comments
 (0)