pub fn primary(os: egui::os::OperatingSystem, mods: egui::Modifiers) -> bool {
if os == egui::os::OperatingSystem::Mac {
mods.mac_cmd
} else {
mods.ctrl || mods.command
}
}
pub fn primary_in(ctx: &egui::Context) -> bool {
let os = ctx.os();
ctx.input(|i| primary(os, i.modifiers))
}
pub fn additive(os: egui::os::OperatingSystem, mods: egui::Modifiers) -> bool {
primary(os, mods) || mods.shift
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RowClick {
Replace,
Toggle,
Range,
AddRange,
}
impl RowClick {
pub fn of(os: egui::os::OperatingSystem, mods: egui::Modifiers) -> Self {
match (primary(os, mods), mods.shift) {
(false, false) => Self::Replace,
(true, false) => Self::Toggle,
(false, true) => Self::Range,
(true, true) => Self::AddRange,
}
}
pub fn in_ctx(ctx: &egui::Context) -> Self {
let os = ctx.os();
ctx.input(|i| Self::of(os, i.modifiers))
}
pub fn is_range(self) -> bool {
matches!(self, Self::Range | Self::AddRange)
}
pub fn sets_anchor(self) -> bool {
!self.is_range()
}
}
pub fn range<'a, T: PartialEq>(rows: &'a [T], anchor: &T, clicked: &T) -> Option<&'a [T]> {
let a = rows.iter().position(|r| r == anchor)?;
let b = rows.iter().position(|r| r == clicked)?;
Some(&rows[a.min(b)..=a.max(b)])
}
#[derive(Debug, Default)]
pub struct MacSecondary {
turned: bool,
}
impl MacSecondary {
pub fn rewrite(&mut self, os: egui::os::OperatingSystem, raw: &mut egui::RawInput) {
if os != egui::os::OperatingSystem::Mac {
self.turned = false;
return;
}
for event in raw.events.iter_mut() {
if let egui::Event::PointerButton { button, pressed, modifiers, .. } = event {
if *button != egui::PointerButton::Primary {
continue;
}
if *pressed {
if modifiers.ctrl && !modifiers.mac_cmd {
*button = egui::PointerButton::Secondary;
self.turned = true;
}
} else if std::mem::take(&mut self.turned) {
*button = egui::PointerButton::Secondary;
}
}
}
}
}
pub fn rewrite_raw(ctx: &egui::Context, raw: &mut egui::RawInput) {
let id = egui::Id::new("brep-mac-secondary-turned");
let os = ctx.os();
let turned = ctx.data(|d| d.get_temp::<bool>(id)).unwrap_or(false);
let mut rewrite = MacSecondary { turned };
rewrite.rewrite(os, raw);
if rewrite.turned != turned {
ctx.data_mut(|d| d.insert_temp(id, rewrite.turned));
}
}