mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-01 14:21:16 +00:00
Janitor: Replace float comparison dance with approx_eq from euclid
Sixtyfps uses euclid already, so let's use euclid for float comparisons as well. I changed the code to decide whether a number is a positive integer to make do without a comparison along the way.
This commit is contained in:
parent
e45635656d
commit
02bdcbf6ff
6 changed files with 11 additions and 11 deletions
|
@ -493,12 +493,11 @@ fn eval_const_expr(
|
||||||
) -> Option<u16> {
|
) -> Option<u16> {
|
||||||
match expression {
|
match expression {
|
||||||
Expression::NumberLiteral(v, Unit::None) => {
|
Expression::NumberLiteral(v, Unit::None) => {
|
||||||
let r = *v as u16;
|
if *v < 0. || *v > u16::MAX as f64 || v.trunc() != *v {
|
||||||
if (r as f32 - *v as f32).abs() > f32::EPSILON {
|
|
||||||
diag.push_error(format!("'{}' must be a positive integer", name), span);
|
diag.push_error(format!("'{}' must be a positive integer", name), span);
|
||||||
None
|
None
|
||||||
} else {
|
} else {
|
||||||
Some(r)
|
Some(*v as u16)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Expression::Cast { from, .. } => eval_const_expr(from, name, span, diag),
|
Expression::Cast { from, .. } => eval_const_expr(from, name, span, diag),
|
||||||
|
|
|
@ -20,6 +20,7 @@ use std::cell::RefCell;
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use euclid::approxeq::ApproxEq;
|
||||||
use sixtyfps_corelib::graphics::{
|
use sixtyfps_corelib::graphics::{
|
||||||
Brush, Color, FontRequest, Image, IntRect, Point, Rect, RenderingCache, Size,
|
Brush, Color, FontRequest, Image, IntRect, Point, Rect, RenderingCache, Size,
|
||||||
};
|
};
|
||||||
|
@ -457,9 +458,7 @@ fn rect_with_radius_to_path(rect: Rect, border_radius: f32) -> femtovg::Path {
|
||||||
// If we're drawing a circle, use directly connected bezier curves instead of
|
// If we're drawing a circle, use directly connected bezier curves instead of
|
||||||
// ones with intermediate LineTo verbs, as `rounded_rect` creates, to avoid
|
// ones with intermediate LineTo verbs, as `rounded_rect` creates, to avoid
|
||||||
// rendering artifacts due to those edges.
|
// rendering artifacts due to those edges.
|
||||||
if (width - height).abs() < 10.0 * f32::EPSILON
|
if width.approx_eq(&height) && (border_radius * 2.).approx_eq(&width) {
|
||||||
&& (border_radius * 2. - width).abs() < 10.0 * f32::EPSILON
|
|
||||||
{
|
|
||||||
path.circle(x + border_radius, y + border_radius, border_radius);
|
path.circle(x + border_radius, y + border_radius, border_radius);
|
||||||
} else {
|
} else {
|
||||||
path.rounded_rect(x, y, width, height, border_radius);
|
path.rounded_rect(x, y, width, height, border_radius);
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
LICENSE END */
|
LICENSE END */
|
||||||
|
|
||||||
use cpp::*;
|
use cpp::*;
|
||||||
|
use euclid::approxeq::ApproxEq;
|
||||||
use items::{ImageFit, TextHorizontalAlignment, TextVerticalAlignment};
|
use items::{ImageFit, TextHorizontalAlignment, TextVerticalAlignment};
|
||||||
use sixtyfps_corelib::graphics::{Brush, FontRequest, Image, Point, Rect, RenderingCache, Size};
|
use sixtyfps_corelib::graphics::{Brush, FontRequest, Image, Point, Rect, RenderingCache, Size};
|
||||||
use sixtyfps_corelib::input::{InternalKeyCode, KeyEvent, KeyEventType, MouseEvent};
|
use sixtyfps_corelib::input::{InternalKeyCode, KeyEvent, KeyEventType, MouseEvent};
|
||||||
|
@ -889,8 +890,8 @@ impl QtItemRenderer<'_> {
|
||||||
rect.is_valid()
|
rect.is_valid()
|
||||||
&& (rect.x != 0.
|
&& (rect.x != 0.
|
||||||
|| rect.y != 0.
|
|| rect.y != 0.
|
||||||
|| (rect.width - target_width).abs() > 100. * f64::EPSILON
|
|| rect.width.approx_eq(&target_width)
|
||||||
|| (rect.height - target_height).abs() > 100. * f64::EPSILON)
|
|| rect.height.approx_eq(&target_height))
|
||||||
});
|
});
|
||||||
let source_size = if !has_source_clipping {
|
let source_size = if !has_source_clipping {
|
||||||
Some(qttypes::QSize { width: target_width as u32, height: target_height as u32 })
|
Some(qttypes::QSize { width: target_width as u32, height: target_height as u32 })
|
||||||
|
|
|
@ -11,7 +11,6 @@ LICENSE END */
|
||||||
#![allow(unused)]
|
#![allow(unused)]
|
||||||
|
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use std::f32::EPSILON;
|
|
||||||
|
|
||||||
use derive_more::*;
|
use derive_more::*;
|
||||||
use serde::Deserialize;
|
use serde::Deserialize;
|
||||||
|
@ -77,7 +76,7 @@ pub struct Color {
|
||||||
|
|
||||||
impl Color {
|
impl Color {
|
||||||
pub fn is_transparent(&self) -> bool {
|
pub fn is_transparent(&self) -> bool {
|
||||||
self.a <= f32::EPSILON
|
self.a.approx_eq(&0.)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -24,6 +24,7 @@ sixtyfps-rendering-backend-default = { version = "=0.1.0", path="../../sixtyfps_
|
||||||
lsp-types = "0.89"
|
lsp-types = "0.89"
|
||||||
lsp-server = "0.5"
|
lsp-server = "0.5"
|
||||||
crossbeam-channel = "0.5" # must match the version used by lsp-server
|
crossbeam-channel = "0.5" # must match the version used by lsp-server
|
||||||
|
euclid = "0.22"
|
||||||
serde_json = "1.0.60"
|
serde_json = "1.0.60"
|
||||||
serde = "1.0.118"
|
serde = "1.0.118"
|
||||||
spin_on = "0.1"
|
spin_on = "0.1"
|
||||||
|
|
|
@ -14,6 +14,7 @@ mod lsp_ext;
|
||||||
mod preview;
|
mod preview;
|
||||||
mod util;
|
mod util;
|
||||||
|
|
||||||
|
use euclid::approxeq::ApproxEq;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
use structopt::StructOpt;
|
use structopt::StructOpt;
|
||||||
|
|
||||||
|
@ -271,7 +272,7 @@ fn handle_request(
|
||||||
// representation.
|
// representation.
|
||||||
let requested_color = params.color;
|
let requested_color = params.color;
|
||||||
|
|
||||||
let color_literal = if (requested_color.alpha - 1.).abs() > 10.0 * f32::EPSILON {
|
let color_literal = if requested_color.alpha.approx_eq(&1.) {
|
||||||
format!(
|
format!(
|
||||||
"#{:0>2x}{:0>2x}{:0>2x}{:0>2x}",
|
"#{:0>2x}{:0>2x}{:0>2x}{:0>2x}",
|
||||||
(requested_color.red * 255.) as u8,
|
(requested_color.red * 255.) as u8,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue