Skip to content

Commit

Permalink
Update GTK packages, resolve clippy 0.1.76 lints
Browse files Browse the repository at this point in the history
Channels were removed from glib 0.19, and so we use the async-channel
crate instead (see gtk-rs/gtk-rs-core#1213).

It took some experimentation on my part to implement this transition;
see https://gtk-rs.org/gtk4-rs/stable/latest/book/main_event_loop.html
for examples that helped steer me in the right direction.
  • Loading branch information
robmoss committed Feb 9, 2024
1 parent 01bbc1e commit f722066
Show file tree
Hide file tree
Showing 26 changed files with 341 additions and 382 deletions.
543 changes: 252 additions & 291 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ default-members = ["crates/*", "."]
[dependencies]
navig18xx = { path = "./crates/navig18xx", version = "0.1.0" }
# Include support for saving surfaces as PNG images.
cairo-rs = { version = "0.18", features = ["png", "pdf", "svg"] }
gtk4 = "0.7"
gdk4 = "0.7"
glib = "0.18"
cairo-rs = { version = "0.19", features = ["png", "pdf", "svg"] }
gtk4 = "0.8"
gdk4 = "0.8"
glib = "0.19"
chrono = "0.4"
log = "0.4"
env_logger = "0.10"
async-channel = "2.2"

[dev-dependencies]
# NOTE: list additional dependencies required by any of the examples here.
Expand Down
2 changes: 1 addition & 1 deletion crates/n18brush/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ n18route = { path = "../n18route", version = "0.1.0" }
n18tile = { path = "../n18tile", version = "0.1.0" }
n18token = { path = "../n18token", version = "0.1.0" }
# Include support for saving surfaces as PNG images.
cairo-rs = { version = "0.18", features = ["png", "pdf", "svg"] }
cairo-rs = { version = "0.19", features = ["png", "pdf", "svg"] }
log = "0.4"
2 changes: 1 addition & 1 deletion crates/n18example/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ n18game = { path = "../n18game", version = "0.1.0" }
n18brush = { path = "../n18brush", version = "0.1.0" }

# Include support for saving surfaces as PNG images.
cairo-rs = { version = "0.18", features = ["png", "pdf", "svg"] }
cairo-rs = { version = "0.19", features = ["png", "pdf", "svg"] }
8 changes: 4 additions & 4 deletions crates/n18game/src/_1861.rs
Original file line number Diff line number Diff line change
Expand Up @@ -976,7 +976,7 @@ fn central_asia(hex: &Hex, suffixes: &[&str]) -> Vec<Tile> {
use n18tile::*;

let name = "Central Asia";
let revenues = vec![10, 20, 30, 40];
let revenues = [10, 20, 30, 40];
revenues
.iter()
.enumerate()
Expand Down Expand Up @@ -1021,7 +1021,7 @@ fn romania(hex: &Hex, suffixes: &[&str]) -> Vec<Tile> {
use n18tile::*;

let name = "Romania";
let revenues = vec![10, 20, 30, 30];
let revenues = [10, 20, 30, 30];
revenues
.iter()
.enumerate()
Expand Down Expand Up @@ -1070,7 +1070,7 @@ fn caucasus(hex: &Hex, suffixes: &[&str]) -> Vec<Tile> {
use n18tile::*;

let name = "Caucasus";
let revenues = vec![10, 20, 40, 60];
let revenues = [10, 20, 40, 60];
revenues
.iter()
.enumerate()
Expand Down Expand Up @@ -1130,7 +1130,7 @@ fn poland(hex: &Hex, suffixes: &[&str]) -> Vec<Tile> {
use n18tile::*;

let name = "Poland";
let revenues = vec![30, 40, 50, 70];
let revenues = [30, 40, 50, 70];
revenues
.iter()
.enumerate()
Expand Down
14 changes: 7 additions & 7 deletions crates/n18game/src/_1867.rs
Original file line number Diff line number Diff line change
Expand Up @@ -809,7 +809,7 @@ fn starting_city_tiles(hex: &Hex) -> Vec<Tile> {
.label(Label::City("O".to_string()), oy_o_label_pos)
.label(Label::MapLocation(name.to_string()), city_label_pos)
}))
.chain(vec![toronto, montreal, timmins_yw, timmins_gr].into_iter())
.chain([toronto, montreal, timmins_yw, timmins_gr])
.collect()
}

Expand Down Expand Up @@ -856,7 +856,7 @@ fn sault_ste_marie(hex: &Hex, suffixes: &[&str]) -> Vec<Tile> {
use n18tile::*;

let name = "Sault Ste Marie";
vec![20, 30, 40, 40]
[20, 30, 40, 40]
.iter()
.enumerate()
.map(|(ix, &revenue)| {
Expand Down Expand Up @@ -890,7 +890,7 @@ fn maritime_provinces(hex: &Hex, suffixes: &[&str]) -> Vec<Tile> {
use n18tile::*;

let name = "Maritime Provinces";
vec![30, 30, 40, 40]
[30, 30, 40, 40]
.iter()
.enumerate()
.map(|(ix, &revenue)| {
Expand Down Expand Up @@ -924,7 +924,7 @@ fn maine(hex: &Hex, suffixes: &[&str]) -> Vec<Tile> {
use n18tile::*;

let name = "Maine";
vec![20, 30, 40, 40]
[20, 30, 40, 40]
.iter()
.enumerate()
.map(|(ix, &revenue)| {
Expand Down Expand Up @@ -958,7 +958,7 @@ fn new_england(hex: &Hex, suffixes: &[&str]) -> Vec<Tile> {
use n18tile::*;

let name = "New England";
vec![30, 40, 50, 60]
[30, 40, 50, 60]
.iter()
.enumerate()
.map(|(ix, &revenue)| {
Expand Down Expand Up @@ -989,7 +989,7 @@ fn buffalo(hex: &Hex, suffixes: &[&str]) -> Vec<Tile> {
use n18tile::*;

let name = "Buffalo";
vec![30, 40, 50, 60]
[30, 40, 50, 60]
.iter()
.enumerate()
.map(|(ix, &revenue)| {
Expand Down Expand Up @@ -1020,7 +1020,7 @@ fn detroit(hex: &Hex, suffixes: &[&str]) -> Vec<Tile> {
use n18tile::*;

let name = "Detroit";
let mut tiles: Vec<Tile> = vec![30, 40, 50, 70]
let mut tiles: Vec<Tile> = [30, 40, 50, 70]
.iter()
.enumerate()
.map(|(ix, &revenue)| {
Expand Down
2 changes: 1 addition & 1 deletion crates/n18game/src/_1889.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ impl super::Game for Game {
self.phase = phase;

let suffix = match phase {
0 | 1 | 2 => "Yw",
0..=2 => "Yw",
_ => "Bn",
};

Expand Down
2 changes: 1 addition & 1 deletion crates/n18game/src/_1889/tiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ pub fn initial_tiles(hex: &Hex) -> Vec<Tile> {

/// Returns the tiles for each off-board location.
pub fn offboard_tiles(hex: &Hex) -> Vec<Tile> {
let suffixes = vec!["Yw", "Bn"];
let suffixes = ["Yw", "Bn"];

let imabari: Vec<Tile> = suffixes
.iter()
Expand Down
6 changes: 3 additions & 3 deletions crates/n18hex/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ categories = ["algorithms", "graphics"]

[dependencies]
# Include support for saving surfaces as PNG images.
cairo-rs = { version = "0.18", features = ["png", "pdf", "svg"] }
pango = "0.18"
pangocairo = "0.18"
cairo-rs = { version = "0.19", features = ["png", "pdf", "svg"] }
pango = "0.19"
pangocairo = "0.19"
11 changes: 6 additions & 5 deletions crates/n18hex/src/theme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use std::collections::BTreeMap;

use crate::{Coord, Hex, HexColour};
use cairo::Context;
use pangocairo::functions::{create_layout, show_layout, update_layout};

/// Defines relative and absolute lengths.
#[derive(Clone, Copy, Debug, PartialEq)]
Expand Down Expand Up @@ -233,8 +234,8 @@ impl<'a> Labeller<'a> {
self.context.move_to(coord.x, coord.y);
self.colour.apply_colour(self.context);
self.layout.set_text(text);
pangocairo::update_layout(self.context, &self.layout);
pangocairo::show_layout(self.context, &self.layout);
update_layout(self.context, &self.layout);
show_layout(self.context, &self.layout);
self.context.new_path();
}

Expand All @@ -256,8 +257,8 @@ impl<'a> Labeller<'a> {
self.context.rel_move_to(coord.x, coord.y);
self.colour.apply_colour(self.context);
self.layout.set_text(text);
pangocairo::update_layout(self.context, &self.layout);
pangocairo::show_layout(self.context, &self.layout);
update_layout(self.context, &self.layout);
show_layout(self.context, &self.layout);
self.context.new_path();
true
}
Expand Down Expand Up @@ -355,7 +356,7 @@ impl Text {
pub fn labeller<'a>(&self, ctx: &'a Context, hex: &Hex) -> Labeller<'a> {
let scale = hex.max_d / 125.0;
let font_descr = self.describe(scale);
let layout = pangocairo::create_layout(ctx);
let layout = create_layout(ctx);
layout.set_font_description(Some(&font_descr));
layout.set_alignment(self.align);
layout.set_wrap(self.wrap);
Expand Down
2 changes: 1 addition & 1 deletion crates/n18map/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ n18tile = { path = "../n18tile", version = "0.1.0" }
n18token = { path = "../n18token", version = "0.1.0" }
n18catalogue = { path = "../n18catalogue", version = "0.1.0" }
# Include support for saving surfaces as PNG images.
cairo-rs = { version = "0.18", features = ["png", "pdf", "svg"] }
cairo-rs = { version = "0.19", features = ["png", "pdf", "svg"] }
7 changes: 2 additions & 5 deletions crates/n18map/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -502,10 +502,7 @@ impl Map {
/// (i.e., you can place an "O" tile on top of a "Y" tile, if the hex has
/// both "O" and "Y" labels).
pub fn add_label_at(&mut self, addr: HexAddress, label: Label) {
self.labels_tbl
.entry(addr)
.or_insert_with(Vec::new)
.push(label)
self.labels_tbl.entry(addr).or_default().push(label)
}

/// Returns the labels associated with the specified map hex.
Expand Down Expand Up @@ -1190,7 +1187,7 @@ mod tests {

// NOTE: iterate over starting connection and, for each, check that it
// has the expected connections, and only the expected connections.
let starts = vec![
let starts = [
(HexAddress::new(0, 0), Connection::City { ix: 0 }),
(HexAddress::new(0, 1), Connection::City { ix: 0 }),
(HexAddress::new(1, 1), Connection::City { ix: 0 }),
Expand Down
2 changes: 1 addition & 1 deletion crates/n18route/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@ rayon = "1.7"
[dev-dependencies]
n18catalogue = { path = "../n18catalogue", version = "0.1.0" }
n18brush = { path = "../n18brush", version = "0.1.0" }
cairo-rs = { version = "0.18" }
cairo-rs = { version = "0.19" }
env_logger = "0.10"
2 changes: 1 addition & 1 deletion crates/n18route/src/train.rs
Original file line number Diff line number Diff line change
Expand Up @@ -539,7 +539,7 @@ fn best_stop_ixs(
.filter(|(ix, _revenue)| !must_not_skip_ixs.contains(ix))
.take(new_num_to_keep)
.map(|(ix, _rev)| *ix)
.chain(new_stop_ixs.into_iter())
.chain(new_stop_ixs)
.collect();
let new_ixs: Vec<usize> = must_stop_ixs
.iter()
Expand Down
2 changes: 1 addition & 1 deletion crates/n18tile/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ categories = ["algorithms", "graphics"]
[dependencies]
n18hex = { path = "../n18hex", version = "0.1.0" }
# Include support for saving surfaces as PNG images.
cairo-rs = { version = "0.18", features = ["png", "pdf", "svg"] }
cairo-rs = { version = "0.19", features = ["png", "pdf", "svg"] }

[[example]]
name = "custom_tile"
Expand Down
2 changes: 1 addition & 1 deletion crates/n18tile/src/city.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ impl City {
// Each circle is centred at the tip of an equilateral triangle
// with side length 2 * radius; it has height radius * sqrt(3).
let half_height = radius * (3.0_f64).sqrt() / 2.0;
let centres = vec![
let centres = [
(-radius, half_height),
(radius, half_height),
(0.0, -half_height),
Expand Down
3 changes: 1 addition & 2 deletions crates/n18tile/src/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,7 @@ impl Connections {
Some(conns) => conns.iter().collect(),
None => vec![],
};
while !to_visit.is_empty() {
let conn = to_visit.pop().unwrap();
while let Some(conn) = to_visit.pop() {
if visited.contains(conn) {
continue;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/n18token/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ categories = ["algorithms", "graphics"]
[dependencies]
n18hex = { path = "../n18hex", version = "0.1.0" }
# Include support for saving surfaces as PNG images.
cairo-rs = { version = "0.18", features = ["png", "pdf", "svg"] }
cairo-rs = { version = "0.19", features = ["png", "pdf", "svg"] }
2 changes: 1 addition & 1 deletion crates/n18token/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl From<Vec<(String, Token)>> for Tokens {

impl From<Tokens> for Vec<(String, Token)> {
fn from(src: Tokens) -> Self {
src.names.into_iter().zip(src.tokens.into_iter()).collect()
src.names.into_iter().zip(src.tokens).collect()
}
}

Expand Down
10 changes: 6 additions & 4 deletions crates/n18ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ n18game = { path = "../n18game", version = "0.1.0" }
n18brush = { path = "../n18brush", version = "0.1.0" }

# Include support for saving surfaces as PNG images.
cairo-rs = { version = "0.18", features = ["png", "pdf", "svg"] }
gtk4 = "0.7"
gdk4 = "0.7"
glib = "0.18"
cairo-rs = { version = "0.19", features = ["png", "pdf", "svg"] }
gtk4 = "0.8"
gdk4 = "0.8"
glib = "0.19"
chrono = "0.4"
log = "0.4"

async-channel = "2.2"
11 changes: 6 additions & 5 deletions crates/n18ui/src/canvas.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use log::info;
use std::sync::{Arc, RwLock};
use std::rc::Rc;
use std::sync::RwLock;

use crate::{Assets, State};
use n18hex::Hex;
Expand Down Expand Up @@ -55,7 +56,7 @@ pub fn max_surface_dims(

pub struct Canvas {
// NOTE: we need to share the surface with the main event loop and the UI.
surface: Arc<RwLock<cairo::ImageSurface>>,
surface: Rc<RwLock<cairo::ImageSurface>>,
context: cairo::Context,
width: i32,
height: i32,
Expand All @@ -68,7 +69,7 @@ impl Canvas {
.expect("Could not create ImageSurface");
let context = cairo::Context::new(&surface)
.expect("Could not create cairo::Context");
let surface = Arc::new(RwLock::new(surface));
let surface = Rc::new(RwLock::new(surface));
Canvas {
surface,
context,
Expand All @@ -77,8 +78,8 @@ impl Canvas {
}
}

pub fn surface(&self) -> Arc<RwLock<cairo::ImageSurface>> {
Arc::clone(&self.surface)
pub fn surface(&self) -> Rc<RwLock<cairo::ImageSurface>> {
Rc::clone(&self.surface)
}

pub fn copy_surface(&self) -> cairo::ImageSurface {
Expand Down
7 changes: 3 additions & 4 deletions crates/n18ui/src/control.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use _gtk::GtkController;

#[derive(Clone)]
pub enum PingSender {
Glib(glib::Sender<PingDest>),
Async(async_channel::Sender<PingDest>),
Mpsc(std::sync::mpsc::Sender<PingDest>),
IgnorePings,
}
Expand All @@ -22,11 +22,10 @@ impl PingSender {
) -> Result<(), Box<dyn std::error::Error>> {
use PingSender::*;
match self {
Glib(sender) => sender.send(dest),
Mpsc(sender) => sender.send(dest),
Async(sender) => sender.send_blocking(dest).map_err(|e| e.into()),
Mpsc(sender) => sender.send(dest).map_err(|e| e.into()),
IgnorePings => Ok(()),
}
.map_err(|e| e.into())
}
}

Expand Down
6 changes: 3 additions & 3 deletions crates/n18ui/src/control/_gtk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,14 @@ pub fn game_file_filters() -> Vec<gtk::FileFilter> {
pub struct GtkController {
window: gtk::Window,
draw_area: gtk::DrawingArea,
ping_tx: glib::Sender<PingDest>,
ping_tx: async_channel::Sender<PingDest>,
}

impl GtkController {
pub fn new(
window: gtk::Window,
draw_area: gtk::DrawingArea,
ping_tx: glib::Sender<PingDest>,
ping_tx: async_channel::Sender<PingDest>,
) -> Self {
GtkController {
window,
Expand Down Expand Up @@ -452,7 +452,7 @@ impl UiController for GtkController {
}

fn ping_tx(&self) -> PingSender {
PingSender::Glib(self.ping_tx.clone())
PingSender::Async(self.ping_tx.clone())
}

fn select_string<F>(&mut self, title: &str, strings: &[&str], callback: F)
Expand Down
2 changes: 1 addition & 1 deletion examples/1867_bc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ fn save_1867_bc_routes(
// Save an image of the map prior to drawing any routes.
state.example.draw_map();
let out_file = image_dir.join("1867_bc.png");
save_png(&state.example, &out_file);
save_png(&state.example, out_file);

// Draw the best routes for each company in turn.
for company in &state.companies {
Expand Down
Loading

0 comments on commit f722066

Please sign in to comment.