Skip to content

Commit

Permalink
feat: load cursor from data
Browse files Browse the repository at this point in the history
I want to add a function to load cursor from data
  • Loading branch information
Decodetalkers committed Dec 8, 2023
1 parent fdd88c4 commit ce0c66b
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions wayland-cursor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,48 @@ impl CursorTheme {
}
}

/// try to load data from theme, else, it will try to load from fallback data
pub fn get_cursor_with_default_data(&mut self, name: &str, data: &[u8]) -> Option<&Cursor> {
match self.cursors.iter().position(|cursor| cursor.name == name) {
Some(i) => Some(&self.cursors[i]),

Check warning on line 183 in wayland-cursor/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

wayland-cursor/src/lib.rs#L181-L183

Added lines #L181 - L183 were not covered by tests
None => {
let cursor = self.load_cursor_with_default_data(name, self.size, data)?;
self.cursors.push(cursor);
self.cursors.iter().last()

Check warning on line 187 in wayland-cursor/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

wayland-cursor/src/lib.rs#L185-L187

Added lines #L185 - L187 were not covered by tests
}
}
}

Check warning on line 190 in wayland-cursor/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

wayland-cursor/src/lib.rs#L190

Added line #L190 was not covered by tests

fn load_cursor_with_default_data(
&mut self,
name: &str,
size: u32,
data: &[u8],
) -> Option<Cursor> {
let conn = Connection::from_backend(self.backend.upgrade()?);

Check warning on line 198 in wayland-cursor/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

wayland-cursor/src/lib.rs#L192-L198

Added lines #L192 - L198 were not covered by tests
'fallback: {
let Some(icon_path) = XCursorTheme::load(&self.name).load_icon(name) else {
break 'fallback;

Check warning on line 201 in wayland-cursor/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

wayland-cursor/src/lib.rs#L200-L201

Added lines #L200 - L201 were not covered by tests
};
let Ok(mut icon_file) = File::open(icon_path) else {
break 'fallback;

Check warning on line 204 in wayland-cursor/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

wayland-cursor/src/lib.rs#L203-L204

Added lines #L203 - L204 were not covered by tests
};
let mut buf = Vec::new();
let Some(images) = ({
if icon_file.read_to_end(&mut buf).is_err() {
break 'fallback;
}
xparser::parse_xcursor(&buf)

Check warning on line 211 in wayland-cursor/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

wayland-cursor/src/lib.rs#L206-L211

Added lines #L206 - L211 were not covered by tests
}) else {
break 'fallback;

Check warning on line 213 in wayland-cursor/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

wayland-cursor/src/lib.rs#L213

Added line #L213 was not covered by tests
};

return Some(Cursor::new(&conn, name, self, &images, size));

Check warning on line 216 in wayland-cursor/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

wayland-cursor/src/lib.rs#L216

Added line #L216 was not covered by tests
}
let images = xparser::parse_xcursor(data)?;
Some(Cursor::new(&conn, name, self, &images, size))
}

Check warning on line 220 in wayland-cursor/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

wayland-cursor/src/lib.rs#L218-L220

Added lines #L218 - L220 were not covered by tests

/// This function loads a cursor, parses it and pushes the images onto the shm pool.
///
/// Keep in mind that if the cursor is already loaded, the function will make a duplicate.
Expand Down

0 comments on commit ce0c66b

Please sign in to comment.