Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Combo row formats #278

Merged
merged 14 commits into from
Feb 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ gst = { package = "gstreamer", version = "0.22" }
gst-plugin-gif = "0.12"
gst-plugin-gtk4 = { version = "0.12", features = ["gtk_v4_14"] }
gtk = { package = "gtk4", version = "0.8", features = ["v4_14"] }
num-rational = { version = "0.4", default-features = false }
num-traits = "0.2"
once_cell = "1.19.0"
pulse = { package = "libpulse-binding", version = "2.26.0" }
pulse_glib = { package = "libpulse-glib-binding", version = "2.25.1" }
Expand Down
4 changes: 2 additions & 2 deletions data/io.github.seadve.Kooha.gschema.xml.in
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
<key type="ay" name="saving-location">
<default>b""</default>
</key>
<key type="u" name="video-framerate">
<default>30</default>
<key type="(ii)" name="framerate">
<default>(30, 1)</default>
</key>
<key type="s" name="screencast-restore-token">
<default>""</default>
Expand Down
1 change: 1 addition & 0 deletions data/resources/resources.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<file compressed="true">profiles.yml</file>
<file compressed="true">style.css</file>
<file compressed="true" preprocess="xml-stripblanks">ui/area-selector.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/item_row.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/preferences-dialog.ui</file>
<file compressed="true" preprocess="xml-stripblanks" alias="gtk/help-overlay.ui">ui/shortcuts.ui</file>
<file compressed="true" preprocess="xml-stripblanks">ui/view-port.ui</file>
Expand Down
31 changes: 31 additions & 0 deletions data/resources/ui/item_row.ui
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<interface>
<template class="KoohaItemRow">
<property name="layout-manager">
<object class="GtkBoxLayout">
<property name="spacing">12</property>
</object>
</property>
<child>
<object class="GtkImage" id="warning_icon">
<property name="icon-name">warning-symbolic</property>
<style>
<class name="warning"/>
</style>
</object>
</child>
<child>
<object class="GtkLabel" id="title_label">
<property name="valign">center</property>
<property name="xalign">0.0</property>
<property name="ellipsize">end</property>
<property name="max-width-chars">20</property>
</object>
</child>
<child>
<object class="GtkImage" id="selected_icon">
<property name="icon-name">object-select-symbolic</property>
</object>
</child>
</template>
</interface>
29 changes: 1 addition & 28 deletions data/resources/ui/preferences-dialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -54,35 +54,8 @@
</object>
</child>
<child>
<object class="AdwActionRow">
<object class="AdwComboRow" id="framerate_row">
<property name="title" translatable="yes">Frame Rate</property>
<child type="suffix">
<object class="GtkBox">
<property name="spacing">12</property>
<child>
<object class="GtkImage" id="framerate_warning">
<property name="tooltip-text" translatable="yes">This frame rate may cause performance issues.</property>
<property name="icon-name">warning-symbolic</property>
<style>
<class name="warning"/>
</style>
</object>
</child>
<child>
<object class="GtkSpinButton" id="framerate_button">
<property name="valign">center</property>
<property name="adjustment">
<object class="GtkAdjustment">
<property name="lower">0</property>
<property name="upper">120</property>
<property name="step-increment">1</property>
<property name="page-increment">5</property>
</object>
</property>
</object>
</child>
</object>
</child>
</object>
</child>
</object>
Expand Down
9 changes: 7 additions & 2 deletions src/area_selector/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,14 @@ use std::{cell::RefCell, os::unix::prelude::RawFd};

pub use self::view_port::Selection;
use self::view_port::ViewPort;
use crate::{application::Application, cancelled::Cancelled, pipeline, screencast_session::Stream};
use crate::{
application::Application,
cancelled::Cancelled,
pipeline::{self, Framerate},
screencast_session::Stream,
};

const PREVIEW_FRAMERATE: u32 = 60;
const PREVIEW_FRAMERATE: Framerate = Framerate::new_raw(60, 1);
const WINDOW_TO_MONITOR_SCALE_FACTOR: f64 = 0.4;

// We can't get header bar height before the window is presented, so we assume "46" as the default.
Expand Down
144 changes: 144 additions & 0 deletions src/framerate_option.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
use std::fmt;

use gtk::{gio, glib::BoxedAnyObject};
use num_traits::Signed;

use crate::{pipeline::Framerate, settings::Settings};

/// The available options for the framerate.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FramerateOption {
_10,
_20,
_24,
_25,
_29_97,
_30,
_48,
_50,
_59_94,
_60,
Other(Framerate),
}

impl FramerateOption {
fn all_except_other() -> [Self; 10] {
[
Self::_10,
Self::_20,
Self::_24,
Self::_25,
Self::_29_97,
Self::_30,
Self::_48,
Self::_50,
Self::_59_94,
Self::_60,
]
}

/// Returns a model of type `BoxedAnyObject`. This contains `Other` if the current settings framerate
/// does not match any of the predefined options.
pub fn model(settings: &Settings) -> gio::ListStore {
let list_store = gio::ListStore::new::<BoxedAnyObject>();

let items = Self::all_except_other()
.into_iter()
.map(BoxedAnyObject::new)
.collect::<Vec<_>>();
list_store.splice(0, 0, &items);

if let other @ Self::Other(_) = Self::from_framerate(settings.framerate()) {
list_store.append(&BoxedAnyObject::new(other));
}

list_store
}

/// Returns the corresponding `FramerateOption` for the given framerate.
pub fn from_framerate(framerate: Framerate) -> Self {
// This must be updated if an option is added or removed.
let epsilon = Framerate::new_raw(1, 100);

Self::all_except_other()
.into_iter()
.find(|o| (o.as_framerate() - framerate).abs() < epsilon)
.unwrap_or(Self::Other(framerate))
}

/// Converts a `FramerateOption` to a framerate.
pub const fn as_framerate(self) -> Framerate {
let (numerator, denominator) = match self {
Self::_10 => (10, 1),
Self::_20 => (20, 1),
Self::_24 => (24, 1),
Self::_25 => (25, 1),
Self::_29_97 => (30_000, 1001),
Self::_30 => (30, 1),
Self::_48 => (48, 1),
Self::_50 => (50, 1),
Self::_59_94 => (60_000, 1001),
Self::_60 => (60, 1),
Self::Other(framerate) => return framerate,
};
Framerate::new_raw(numerator, denominator)
}
}

impl fmt::Display for FramerateOption {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(match self {
Self::_10 => "10",
Self::_20 => "20",
Self::_24 => "24",
Self::_25 => "25",
Self::_29_97 => "29.97",
Self::_30 => "30",
Self::_48 => "48",
Self::_50 => "50",
Self::_59_94 => "59.94",
Self::_60 => "60",
Self::Other(framerate) => return write!(f, "{}", framerate),
})
}
}

#[cfg(test)]
mod tests {
use crate::pipeline::Framerate;

use super::*;

#[track_caller]
fn test_framerate(framerate: Framerate, expected: FramerateOption) {
assert_eq!(FramerateOption::from_framerate(framerate), expected);
}

#[test]
fn framerate_option() {
test_framerate(
Framerate::from_integer(5),
FramerateOption::Other(Framerate::from_integer(5)),
);
test_framerate(Framerate::from_integer(10), FramerateOption::_10);
test_framerate(Framerate::from_integer(20), FramerateOption::_20);
test_framerate(Framerate::from_integer(24), FramerateOption::_24);
test_framerate(Framerate::from_integer(25), FramerateOption::_25);
test_framerate(
Framerate::approximate_float(29.97).unwrap(),
FramerateOption::_29_97,
);
test_framerate(Framerate::from_integer(30), FramerateOption::_30);
test_framerate(Framerate::from_integer(48), FramerateOption::_48);
test_framerate(Framerate::from_integer(50), FramerateOption::_50);
test_framerate(
Framerate::approximate_float(59.94).unwrap(),
FramerateOption::_59_94,
);
test_framerate(Framerate::from_integer(60), FramerateOption::_60);
test_framerate(
Framerate::from_integer(120),
FramerateOption::Other(Framerate::from_integer(120)),
);
}
}
Loading
Loading