Skip to content

Commit 331ac70

Browse files
authored
Merge pull request #16 from typester/feature/view-tags-list
feat: add viewTagsList
2 parents ceef17e + c3a13da commit 331ac70

File tree

1 file changed

+52
-6
lines changed

1 file changed

+52
-6
lines changed

src/gql.rs

Lines changed: 52 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ pub struct OutputState {
6363
pub focused_tags: Option<i32>,
6464
pub focused_tags_list: Option<Vec<i32>>,
6565
pub view_tags: Option<Vec<i32>>,
66+
pub view_tags_list: Option<Vec<i32>>,
6667
pub urgent_tags: Option<i32>,
6768
pub urgent_tags_list: Option<Vec<i32>>,
6869
pub layout_name: Option<String>,
@@ -75,6 +76,7 @@ pub struct GOutputState {
7576
pub focused_tags: Option<i32>,
7677
pub focused_tags_list: Option<Vec<i32>>,
7778
pub view_tags: Option<Vec<i32>>,
79+
pub view_tags_list: Option<Vec<i32>>,
7880
pub urgent_tags: Option<i32>,
7981
pub urgent_tags_list: Option<Vec<i32>>,
8082
pub layout_name: Option<String>,
@@ -94,6 +96,7 @@ impl From<&OutputState> for GOutputState {
9496
focused_tags: state.focused_tags,
9597
focused_tags_list: state.focused_tags_list.clone(),
9698
view_tags: state.view_tags.clone(),
99+
view_tags_list: state.view_tags_list.clone(),
97100
urgent_tags: state.urgent_tags,
98101
urgent_tags_list: state.urgent_tags_list.clone(),
99102
layout_name: state.layout_name.clone(),
@@ -123,6 +126,10 @@ impl GOutputState {
123126
self.view_tags.as_ref()
124127
}
125128

129+
async fn view_tags_list(&self) -> Option<&Vec<i32>> {
130+
self.view_tags_list.as_ref()
131+
}
132+
126133
async fn urgent_tags(&self) -> Option<i32> {
127134
self.urgent_tags
128135
}
@@ -157,6 +164,7 @@ impl RiverSnapshot {
157164
focused_tags: None,
158165
focused_tags_list: None,
159166
view_tags: None,
167+
view_tags_list: None,
160168
urgent_tags: None,
161169
urgent_tags_list: None,
162170
layout_name: None,
@@ -186,8 +194,10 @@ impl RiverSnapshot {
186194
}
187195
OutputViewTags { id, name, tags } => {
188196
let converted = tags.iter().map(|v| *v as i32).collect::<Vec<i32>>();
197+
let list = bit_values_to_tags(&converted);
189198
self.update_output_state(id, name, move |state| {
190-
state.view_tags = Some(converted);
199+
state.view_tags = Some(converted.clone());
200+
state.view_tags_list = Some(list.clone());
191201
});
192202
}
193203
OutputUrgentTags { id, name, tags } => {
@@ -274,10 +284,16 @@ impl RiverSnapshot {
274284

275285
if type_allowed(RiverEventType::OutputViewTags) {
276286
if let Some(tags) = &state.view_tags {
287+
let tags_list = if include_lists {
288+
state.view_tags_list.clone()
289+
} else {
290+
None
291+
};
277292
events.push(RiverEvent::OutputViewTags(GOutputViewTags {
278293
output_id: state.output_id.clone(),
279294
name: state.name.clone(),
280295
tags: tags.clone(),
296+
tags_list,
281297
}));
282298
}
283299
}
@@ -487,6 +503,24 @@ fn bitmask_to_tags(mask: u32) -> Vec<i32> {
487503
.collect()
488504
}
489505

506+
fn bit_values_to_tags(values: &[i32]) -> Vec<i32> {
507+
values
508+
.iter()
509+
.filter_map(|value| {
510+
if *value <= 0 {
511+
None
512+
} else {
513+
let v = *value as u32;
514+
if v.is_power_of_two() {
515+
Some(v.trailing_zeros() as i32)
516+
} else {
517+
None
518+
}
519+
}
520+
})
521+
.collect()
522+
}
523+
490524
#[derive(Union, Clone)]
491525
pub enum RiverEvent {
492526
OutputFocusedTags(GOutputFocusedTags),
@@ -530,13 +564,18 @@ pub struct GOutputViewTags {
530564
pub output_id: ID,
531565
pub name: Option<String>,
532566
pub tags: Vec<i32>,
567+
pub tags_list: Option<Vec<i32>>,
533568
}
534569
#[Object(name = "OutputViewTags")]
535570
impl GOutputViewTags {
536571
async fn tags(&self) -> &Vec<i32> {
537572
&self.tags
538573
}
539574

575+
async fn tags_list(&self) -> Option<&Vec<i32>> {
576+
self.tags_list.as_ref()
577+
}
578+
540579
async fn output_id(&self) -> &ID {
541580
&self.output_id
542581
}
@@ -670,11 +709,16 @@ fn make_river_event(value: river::Event, include_lists: bool) -> RiverEvent {
670709
id: output_id,
671710
name,
672711
tags,
673-
} => RiverEvent::OutputViewTags(GOutputViewTags {
674-
output_id: id_to_graphql(&output_id),
675-
name,
676-
tags: tags.into_iter().map(|v| v as i32).collect::<Vec<i32>>(),
677-
}),
712+
} => {
713+
let tag_values = tags.into_iter().map(|v| v as i32).collect::<Vec<i32>>();
714+
let tags_list = include_lists.then(|| bit_values_to_tags(&tag_values));
715+
RiverEvent::OutputViewTags(GOutputViewTags {
716+
output_id: id_to_graphql(&output_id),
717+
name,
718+
tags: tag_values,
719+
tags_list,
720+
})
721+
}
678722
OutputUrgentTags {
679723
id: output_id,
680724
name,
@@ -748,6 +792,7 @@ impl QueryRoot {
748792
let mut gql = GOutputState::from(state);
749793
if !include_lists {
750794
gql.focused_tags_list = None;
795+
gql.view_tags_list = None;
751796
gql.urgent_tags_list = None;
752797
}
753798
gql
@@ -770,6 +815,7 @@ impl QueryRoot {
770815
let mut gql = GOutputState::from(state);
771816
if !include_lists {
772817
gql.focused_tags_list = None;
818+
gql.view_tags_list = None;
773819
gql.urgent_tags_list = None;
774820
}
775821
gql

0 commit comments

Comments
 (0)