livesplit_core/run/editor/
segment_row.rs

1use super::{parse_positive, Editor, ParseError};
2use crate::{settings::Image, util::PopulateString, TimeSpan};
3
4/// A Segment Row describes the segment in the Run Editor actively selected for
5/// editing.
6pub struct SegmentRow<'editor> {
7    index: usize,
8    editor: &'editor mut Editor,
9}
10
11impl<'a> SegmentRow<'a> {
12    pub(super) fn new(index: usize, editor: &'a mut Editor) -> Self {
13        SegmentRow { index, editor }
14    }
15
16    /// Accesses the icon of the segment.
17    pub fn icon(&self) -> &Image {
18        self.editor.run.segment(self.index).icon()
19    }
20
21    /// Sets the icon of the segment.
22    pub fn set_icon<D: Into<Image>>(&mut self, image: D) {
23        self.editor.run.segment_mut(self.index).set_icon(image);
24        self.editor.raise_run_edited();
25    }
26
27    /// Removes the icon of the segment.
28    pub fn remove_icon(&mut self) {
29        self.editor.run.segment_mut(self.index).set_icon([]);
30        self.editor.raise_run_edited();
31    }
32
33    /// Accesses the name of the segment.
34    pub fn name(&self) -> &str {
35        self.editor.run.segment(self.index).name()
36    }
37
38    /// Sets the name of the segment.
39    pub fn set_name<S>(&mut self, name: S)
40    where
41        S: PopulateString,
42    {
43        self.editor.run.segment_mut(self.index).set_name(name);
44        self.editor.raise_run_edited();
45    }
46
47    /// Accesses the split time of the segment for the active timing method.
48    pub fn split_time(&self) -> Option<TimeSpan> {
49        let method = self.editor.selected_method;
50        self.editor
51            .run
52            .segment(self.index)
53            .personal_best_split_time()[method]
54    }
55
56    /// Sets the split time of the segment for the active timing method.
57    pub fn set_split_time(&mut self, time: Option<TimeSpan>) {
58        let method = self.editor.selected_method;
59        self.editor
60            .run
61            .segment_mut(self.index)
62            .personal_best_split_time_mut()[method] = time;
63        self.editor.times_modified();
64        self.editor.fix();
65    }
66
67    /// Parses a split time from a string and sets it for the active timing
68    /// method.
69    pub fn parse_and_set_split_time(&mut self, time: &str) -> Result<(), ParseError> {
70        self.set_split_time(parse_positive(time)?);
71        Ok(())
72    }
73
74    /// Accesses the segment time of the segment for the active timing method.
75    pub fn segment_time(&self) -> Option<TimeSpan> {
76        self.editor.segment_times[self.index]
77    }
78
79    /// Sets the segment time of the segment for the active timing method.
80    pub fn set_segment_time(&mut self, time: Option<TimeSpan>) {
81        self.editor.segment_times[self.index] = time;
82        self.editor.fix_splits_from_segments();
83        self.editor.times_modified();
84        self.editor.fix();
85    }
86
87    /// Parses a segment time from a string and sets it for the active timing
88    /// method.
89    pub fn parse_and_set_segment_time(&mut self, time: &str) -> Result<(), ParseError> {
90        self.set_segment_time(parse_positive(time)?);
91        Ok(())
92    }
93
94    /// Accesses the best segment time of the segment for the active timing method.
95    pub fn best_segment_time(&self) -> Option<TimeSpan> {
96        let method = self.editor.selected_method;
97        self.editor.run.segment(self.index).best_segment_time()[method]
98    }
99
100    /// Sets the best segment time of the segment for the active timing method.
101    pub fn set_best_segment_time(&mut self, time: Option<TimeSpan>) {
102        let method = self.editor.selected_method;
103        self.editor
104            .run
105            .segment_mut(self.index)
106            .best_segment_time_mut()[method] = time;
107        self.editor.times_modified();
108        self.editor.fix();
109    }
110
111    /// Parses a best segment time from a string and sets it for the active
112    /// timing method.
113    pub fn parse_and_set_best_segment_time(&mut self, time: &str) -> Result<(), ParseError> {
114        self.set_best_segment_time(parse_positive(time)?);
115        Ok(())
116    }
117
118    /// Accesses the provided comparison's time of the segment for the active
119    /// timing method.
120    pub fn comparison_time(&self, comparison: &str) -> Option<TimeSpan> {
121        let method = self.editor.selected_method;
122        self.editor.run.segment(self.index).comparison(comparison)[method]
123    }
124
125    /// Sets the provided comparison's time of the segment for the active timing method.
126    pub fn set_comparison_time(&mut self, comparison: &str, time: Option<TimeSpan>) {
127        let method = self.editor.selected_method;
128        self.editor
129            .run
130            .segment_mut(self.index)
131            .comparison_mut(comparison)[method] = time;
132        self.editor.times_modified();
133        self.editor.fix();
134    }
135
136    /// Parses a comparison time for the provided comparison and sets it for the
137    /// active timing method.
138    pub fn parse_and_set_comparison_time(
139        &mut self,
140        comparison: &str,
141        time: &str,
142    ) -> Result<(), ParseError> {
143        self.set_comparison_time(comparison, parse_positive(time)?);
144        Ok(())
145    }
146}